我想和两个房间的客户一起加入新房间。有没有办法实现这个目标?像:
io.sockets.clients(room1 , room2).join(room 3);
答案 0 :(得分:4)
var rooms = ['room1', 'room2', room3]; // list of rooms
socket.join('room1'); // assign the room on connection
var clients = io.sockets.clients(rooms[0]); // list of all clients in room1
for (client in clients)
{
clients[client].leave(rooms[0]); // leave room1
clients[client].join(rooms[2]); // join room3
}
对room2重复相同的操作。希望这有帮助
答案 1 :(得分:1)
对于使用socket.IO版本1.0或更高版本的用户,这可能无效。请尝试使用以下代码。
var clients = io.sockets.adapter.rooms['Current Room Name'];
for (var clientId in clients ) {
//this is the socket of each client in the room.
var clientSocket = io.sockets.connected[clientId];
//If you want, you can remove the client form the first(current) room
//clientSocket.leave('current room');
clientSocket.join('new room to join');
}