Reputation: 1954
I have found a few solutions but they seem dates and are not working.
How can I detect what room a user was disconnected from?
Upvotes: 1
Views: 1701
Reputation: 1
in version 4 and above, if you connect to socket socket io automaticlly create a unique socketId. if you have several rooms you must create array of abject and save rooms and it's users(socktIds) in it.
if disconnect a socket you can find easily in that array then you can find the room.
Upvotes: 0
Reputation: 23268
With socket.io 1.0 you can do this with following code:
socket.on('disconnect', function() {
console.log(socket.rooms);
});
It will output an array of rooms the user was joined to.
Note that each socket automatically joins a room named with this socket's ID. So you might see something like
[ 'hIP6r4z8Ym1n5SQUAAAA', 'my_room_name' ]
in your console.
Upvotes: 0
Reputation: 337
I don't know what do you mean about room , but if it is a chat app or something like that you can create a variable on socket object like roomID when first connection.
io.sockets.on("connection",function(socket){
socket.on("new:user",function(userDATA){
// create variable as roomID on socket.
socket.roomID = userDATA.roomID
});
// other events
socket.on("disconnect",function(data){
var roomID = socket.roomID;
// do what you want to do
});
});
Upvotes: 4