I'm starting with socket.io + node.js, I know how to send a message locally and to broadcast socket.broadcast.emit()
function:- all the connected clients receive the same message.
现在,我想知道如何向特定的客户端发送私有消息,我的意思是两个人之间的私有聊天使用一个套接字(客户端到客户端流).谢谢.
I'm starting with socket.io + node.js, I know how to send a message locally and to broadcast socket.broadcast.emit()
function:- all the connected clients receive the same message.
现在,我想知道如何向特定的客户端发送私有消息,我的意思是两个人之间的私有聊天使用一个套接字(客户端到客户端流).谢谢.
当用户连接时,它应该向服务器发送一条用户名必须唯一的消息,比如邮箱.
一对用户名和套接字应存储在如下对象中:
var users = {
'userA@example.com': [socket object],
'userB@example.com': [socket object],
'userC@example.com': [socket object]
}
在客户端上,向服务器发送包含以下数据的对象:
{
to:[the other receiver's username as a string],
from:[the person who sent the message as string],
message:[the message to be sent as string]
}
On the server, listen for messages. When a message is received, emit the data to the receiver.
users[data.to].emit('receivedMessage', data)
在客户机上,监听名为"receivedMessage"的服务器发出的信息,通过读取数据,您可以处理这些信息来自谁以及发送的消息.