4

我想将两个房间的客户加入一个新房间。有没有办法做到这一点?像:

io.sockets.clients(room1 , room2).join(room 3);
4

2 回答 2

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
    }

对房间 2 重复相同的操作。希望这可以帮助

于 2012-06-01T06:11:57.507 回答
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');   

}
于 2014-07-30T04:55:50.657 回答