count users per room (#15)
This commit is contained in:
parent
5edc5874c2
commit
cdcaebafe3
1 changed files with 7 additions and 4 deletions
11
src/index.ts
11
src/index.ts
|
@ -60,11 +60,14 @@ io.on('connection', socket => {
|
|||
users.splice(users.indexOf(socket), 1);
|
||||
});
|
||||
|
||||
const socketsCount = Object.keys(io.in(roomID).sockets).length;
|
||||
const clients = Object.keys(io.sockets.adapter.rooms[roomID].sockets);
|
||||
|
||||
if (socketsCount > userLimit) {
|
||||
users.forEach((user: Socket) => {
|
||||
user.disconnect();
|
||||
if (clients.length > userLimit) {
|
||||
clients.forEach((clientKey: string) => {
|
||||
const clientSocket = io.sockets.connected[clientKey];
|
||||
|
||||
serverDebug(`${clientSocket} has left the ${roomID} room because the user limit was reached.`);
|
||||
clientSocket.leave(roomID);
|
||||
});
|
||||
|
||||
return;
|
||||
|
|
Loading…
Reference in a new issue