Socket.io socket on socket

I have this script with socket.io:

The socket connects and connects to the room, and it is the master. Other sockets are connected to his room. When the master disconnects, I want to knock all other sockets out of this room.

I thought about this:

socket.on('disconnect', function(data){
    // if socket 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.       
});

      

I want to do this without too much logic and for the loops to stop the application. Is something like io.sockets.leave(socket.room)

this possible ?

+2


source to share


2 answers


I'm not 100% sure, but after reading the Socket.IO documentation on github I think this should work:



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

      

+4


source


Alessioalex's response returns an error as it tries to call "leave" on the socket array. Working solution:



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

      

+4


source







All Articles