Node.js 插座断开时的Socket.io

Node.js 插座断开时的Socket.io,node.js,socket.io,Node.js,Socket.io,我对socket.io有这样的场景: 一个插座连接一个房间,他就是主人。其他插座连接他的房间。当主人断开连接时,我想把这个房间里所有的插座都踢出去 我想到了这一点: socket.on('disconnect', function(data){ // if socket's id == room he is the master and kick other sockets from this // room and join them to a room of their

我对socket.io有这样的场景:

一个插座连接一个房间,他就是主人。其他插座连接他的房间。当主人断开连接时,我想把这个房间里所有的插座都踢出去

我想到了这一点:

socket.on('disconnect', function(data){
    // if socket's id == room  he is the master and kick other sockets from this 
    // room and join them to a room of their own identified by their ids.       
});

我希望这样做没有太多的逻辑和循环来暂停应用程序。是否可以执行类似于
io.sockets.leave(socket.room)
的操作

我不是100%确定,但在阅读github之后,我认为这应该是可行的:

io.sockets.in(socket.room).leave(socket.room);

alessioalex的回答返回一个错误,因为它试图对套接字数组调用“leave”。这是一个有效的解决方案:

io.sockets.clients(socket.room).forEach(function(listener) {
    listener.leave(socket.room);
});