Socket.IO Best way to send messages between two users? [closed]

Deadly 提交于 2021-01-08 05:29:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!