问题
I want to know what is the best way to send messages between two users? I know you can do rooms and join them but you have to "create" them first. Think of it like chat messenger. You only show the messages you recieve between those two users.
I can do one big object but eventually that would be a big object.
What are your suggestions on handling this?
回答1:
Every socket in Socket.io has its own ID. You can send messages directly to a socket, once you know that ID. Example from http://socket.io/docs/rooms-and-namespaces/#default-room
io.on('connection', function(socket){
socket.on('say to someone', function(id, msg){
socket.broadcast.to(id).emit('my message', msg);
});
});
来源:https://stackoverflow.com/questions/37392848/socket-io-best-way-to-send-messages-between-two-users