Socket.IO - 如何向房间里的每个人发送事件,包括发件人? [英] Socket.IO - how to emit event to everybody in the room, including sender?
本文介绍了Socket.IO - 如何向房间里的每个人发送事件,包括发件人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的Socket.Io / Node.Js / Express应用程序中 - 在我添加聊天室之前 - 我曾经为每个人(包括发件人)发出这样的事件:
In my Socket.Io / Node.Js / Express app - before I added chat room – I used to emit events like this for everyone (including the sender):
io.emit('chat message', msg);
现在我添加房间并尝试使用
Now I added rooms and try to do the same thing using
socket.broadcast.to(socket.room).emit('chat message', msg);
或
socket.to(socket.room).emit('chat message', msg);
但两者都只发送消息给收件人,但不发送给发件人。
but both only send the message to receivers, but not to the sender.
我应该怎么做才能将此消息发送给发送者,谁也在聊天室?
What should I do so that this message also goes to the sender, who's in the chat room as well?
谢谢!
推荐答案
在这个问题中找到答案:
Socket.io不会向discconect上的房间发送消息
Found an answer in this question: Socket.io Won't Emit messages to rooms on discconect
只需使用
io.sockets.in(socket.room).emit('chat message', msg);
这篇关于Socket.IO - 如何向房间里的每个人发送事件,包括发件人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文