From cdcaebafe3cdd9056f3b96a5f3bc5069970edca8 Mon Sep 17 00:00:00 2001 From: Mihaela Dumitru Date: Tue, 12 Dec 2023 16:04:10 +0200 Subject: [PATCH] count users per room (#15) --- src/index.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index c4ca3da..7cdc296 100644 --- a/src/index.ts +++ b/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;