Created
September 5, 2019 13:37
-
-
Save Noitidart/82ad4ac752e78af3b97a6ad8d6831f15 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns the room names that a socket is connected to. | |
* | |
* @param {string} socketId - a socket id - `sails.sockets.getId(this.req)` | |
* | |
* @returns {string[]} rooms | |
*/ | |
function getSocketRooms(socketId) { | |
return new Promise((resolve, reject) => { | |
sails.io.of('/').adapter.clientRooms(socketId, (err, rooms) => { | |
if (err) { | |
reject(err); | |
} else { | |
// the "||" because if socket is not connected to any rooms, ids is null | |
resolve(rooms || []); | |
} | |
}); | |
}); | |
} | |
/** | |
* Gets the sockets currently connected in room. | |
* | |
* @param {string} room | |
* | |
* @returns {string[]} socketIds | |
*/ | |
function getSocketIdsInRoom(room) { | |
return new Promise((resolve, reject) => { | |
sails.io.in(room).clients((err, socketIds) => { | |
if (err) { | |
reject(err); | |
} else { | |
// the "||" because if room has no sockets in it, | |
resolve(socketIds || []); | |
} | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment