【问题标题】:socket.io join clients from two roomssocket.io 从两个房间加入客户端
【发布时间】:2012-06-06 06:36:40
【问题描述】:

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

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

【问题讨论】:

  • 对socket.io的经验很少,不知道房间是什么。什么是房间(房间类型 1)?你用的是什么库?
  • 我假设房间是指名称空间...实际上我认为这并不像您在上面写的那么容易。您必须向两个房间发送消息才能连接到新创建的房间。

标签: javascript node.js chat socket.io


【解决方案1】:
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 重复相同的操作。希望这会有所帮助

【讨论】:

    【解决方案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');   
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 2017-05-22
      • 1970-01-01
      • 2018-12-01
      • 2018-01-09
      • 2015-01-05
      • 1970-01-01
      相关资源
      最近更新 更多