Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
是否有任何内置方法可以将消息广播到给定用户已加入的所有房间,默认房间除外?我正在使用以下代码片段来实现这一点,但如果可能的话,我想以更简洁的方式实现同样的目标:
for (var room in io.sockets.manager.roomClient[socket.id])) { if (key) { socket.broadcast.to(key).emit('something'); } }
我不确定是否有本地方式,但可能类似于(伪代码):
var rooms = {}; (inside client) on join: rooms[0] = 'roomname'; rooms[1] = 'roomname2'; etc... for(var i = 0; i < rooms.length; i++) { if(rooms[i] != 'default_excludedroomname') { io.sockets.in(rooms[i]).emit('message', etc...); } }