2011-12-15 137 views
2

我有这种情况与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 own identified by their ids.  
}); 

我想这样做,没有太多的逻辑和循环来搪塞应用。是否有可能类似io.sockets.leave(socket.room)

回答

4

我不是100%肯定,但在GitHub上阅读Socket.IO's documentation后,我觉得这应该工作:因为它试图调用“离开”插座的阵列上

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

alessioalex的答案返回一个错误。工作解决方案是:

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

你可以编辑他的答案。 – mak 2013-07-04 10:24:37