使用Socket.io将消息发送到多个房间? [英] Sending messages to multiple rooms using Socket.io?

查看:282
本文介绍了使用Socket.io将消息发送到多个房间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用socket.io将消息发送到多个房间?

Is it possible to send messages to multiple rooms using socket.io?

发送到1个房间:

io.sockets.in(room).emit("id", {})

发送到N个房间:

io.sockets.in(room1, room2, roomN).emit("id", {})


推荐答案

strong> sockets.in 方法仅接受一个房间作为参数,因此要广播到多个房间,您必须在发射之间重置房间。像这样的东西应该起作用:

The sockets.in method only accepts one room as an argument, so to broadcast to multiple rooms you would have to reset the room, in between emissions. Something like this should work:

['room1', 'room2', 'room3'].forEach(function(room){
    io.sockets.in(room).emit("id", {});
});

这篇关于使用Socket.io将消息发送到多个房间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆