我想将两个房间的客户加入一个新房间。有没有办法做到这一点?像:
io.sockets.clients(room1 , room2).join(room 3);
我想将两个房间的客户加入一个新房间。有没有办法做到这一点?像:
io.sockets.clients(room1 , room2).join(room 3);
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
}
对房间 2 重复相同的操作。希望这可以帮助
对于使用 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');
}