在我的 Socket.Io / Node.Js / Express 应用程序中 - 在添加聊天室之前 - 我曾经为每个人(包括发送者)发出这样的事件:
io.emit('chat message', msg);
现在我添加了房间并尝试使用
做同样的事情socket.broadcast.to(socket.room).emit('chat message', msg);
或
socket.to(socket.room).emit('chat message', msg);
但两者都只将消息发送给接收者,但不发送给发送者。
我应该怎么做才能将此消息发送给也在聊天室中的发件人?
在这个问题中找到了答案: Socket.io 不会向 Discconect 上的房间发送消息
简单使用
io.sockets.in(socket.room).emit('chat message', msg);
socket.nsp.to(room).emit(event);