需要与socketio一对一聊天的帮助。 我已经阅读了很多示例,这些示例说明创建一个用于私人聊天的空间。 我总是得到的示例将是这样的:
io.on('connection', socket => {
//Get the chatID of the user and join in a room of the same chatID
chatID = socket.handshake.query.chatID
socket.join(chatID)
//Leave the room if the user closes the socket
socket.on('disconnect', () => {
socket.leave(chatID)
})
//Send message to only a particular user
socket.on('send_message', message => {
receiverChatID = message.receiverChatID
senderChatID = message.senderChatID
content = message.content
//Send message to only that particular room
socket.in(receiverChatID).emit('receive_message', {
'content': content,
'senderChatID': senderChatID,
'receiverChatID':receiverChatID,
})
})
});
但是我无法理解:如在本例中的“ receiverChatID”房间,当前正在向同一接收者发送消息的所有用户[比如说user2]将收到其他用户正在发送的消息的广播。
例如,用户1和用户2正在进行私人聊天对话。聊天室ID为“ user2”。 现在,user3向用户2发送消息,房间名称再次为“ user2”,因此,当广播完成时,用户3和用户1都将接收到消息,即使它们应分别为私有。
一种方法是在两个用户之间创建唯一的聊天室ID,并在其中维护一些数据存储;如果那样做,那么如何创建唯一的聊天室ID,特别是当用户加入并离开并重新加入保存空间。 然后每当两个用户加入时,就查询这个不固定的房间,以供两个用户使用?
说我创建一个地图:{roomid1:MD5 [“ user1”,“ user2”]}。 现在,您如何确定用户1和用户2的顺序(我想是字母顺序),所以对于两对用户,将创建一个唯一的房间ID?
如何解决一对一聊天的问题?