Skip to content

Instantly share code, notes, and snippets.

@stewartcelani
Created September 27, 2016 07:39
Show Gist options
  • Save stewartcelani/c64fb5cc9765ca2c698939a4b3bbbf51 to your computer and use it in GitHub Desktop.
Save stewartcelani/c64fb5cc9765ca2c698939a4b3bbbf51 to your computer and use it in GitHub Desktop.
//Get the room:
var room = io.sockets.adapter.rooms[roomId] || {};
//Declaration of number of users variable
var numUsers;
//If there are any user connected in the room, create the object
if(room.user===undefined)
room.user = {};
//If you join the room and have another opened session, increase the number of sessions
if(room.user[socket.request.user.id] && isConnect)
room.user[socket.request.user.id] += 1;
//If you leave the room and have another opened session, decrease the number of sessions
else if(room.user[socket.request.user.id] && !isConnect){
room.user[socket.request.user.id] -= 1;
//If the number of sessions is 0, delete the user from the object
if(room.user[socket.request.user.id]===0)
delete room.user[socket.request.user.id];
}
//If the user don't have any session and join the room, put 1 session in the object
else if(isConnect){
room.user[socket.request.user.id] = 1;
}
//Get the number of users from room.user object
numUsers = Object.keys(room.user).length;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment