count users per room (#15)

This commit is contained in:
Mihaela Dumitru 2023-12-12 16:04:10 +02:00 committed by GitHub
parent 5edc5874c2
commit cdcaebafe3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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;