-
Notifications
You must be signed in to change notification settings - Fork 0
/
roomsManager.js
40 lines (32 loc) · 1015 Bytes
/
roomsManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const generateRandomString = () => {
const characters = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';
let result = '';
for (let i = 0; i < 6; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
result += characters.charAt(randomIndex);
}
return result;
};
const createRoomId = (io) => {
let possibleRoomId = generateRandomString();
while (io.sockets.adapter.rooms.has(possibleRoomId)) {
possibleRoomId = generateRandomString();
}
return possibleRoomId;
}
const roomExists = (io, roomId) => {
const roomsIter = io.sockets.adapter.rooms.keys();
for (existingRoomId of roomsIter) {
if (roomId === existingRoomId) return true;
}
return false;
}
const listRooms = (io) => {
const roomsIter = io.sockets.adapter.rooms.keys();
let roomNum = 1;
for (roomId of roomsIter) {
console.log(`Room ${roomNum}: ${roomId}`);
roomNum++;
}
}
module.exports = { listRooms, createRoomId, roomExists };