[英]Emitting to specific room of socket.io 1.X namespace
I've implemented a number of socket.io namespaces that a user can connect to based on the page they are on. 我已经实现了许多socket.io命名空间,用户可以根据其所在的页面连接到该命名空间。
Emitting data to a particular namespace:room combination seems to require excess boilerplate. 将数据发送到特定的名称空间:房间组合似乎需要多余的样板。
This is how I'm currently setting up the namespace and emitting events to sockets in a given room: 这是我当前设置命名空间并将事件发送到给定房间中的套接字的方式:
1) Setting up the namespace 1)设置名称空间
let newsNs = io.of('/news');
newsNs.on(CONNECTION, socket => {
socket.join('political');
});
2) Emitting an event to sockets in the /news
namespace and political
room: 2)向
/news
命名空间和political
空间中的套接字发出事件:
newsNs.in('political').clients((err, clients) => {
clients.forEach(c => newsNs.connected[c].emit(EVENT, 'data'));
})
Is there a cleaner and more concise way to emit to all sockets in a namespace:room combination without first iterating over each client and then deriving the sockets from someNs.connected? 是否有一种更干净,更简洁的方式来发送到namespace:room组合中的所有套接字,而无需首先遍历每个客户端,然后从someNs.connected派生套接字?
A simple newsNs.to('political').emit(payload)
should suffice. 一个简单的
newsNs.to('political').emit(payload)
就足够了。
Check out http://socket.io/docs/rooms-and-namespaces/#joining-and-leaving as an example. 以http://socket.io/docs/rooms-and-namespaces/#joining-and-leaving为例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.