-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroom_service.js
More file actions
50 lines (47 loc) · 1.14 KB
/
room_service.js
File metadata and controls
50 lines (47 loc) · 1.14 KB
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
41
42
43
44
45
46
47
48
49
50
/**
@type {SocketIO.Server}
*/
let _io;
const log = require('./log.js');
const MAX_CLIENTS = 2;
/**
@param {SocketIO.Socket} socket
*/
let listen = socket => {
const io = _io;
const rooms = io.nsps["/"].adapter.rooms;
socket.on("join", room => {
let numClients = 0;
if (rooms[room]) {
numClients = rooms[room].length;
}
if (numClients < MAX_CLIENTS) {
socket.on("ready", () => {
log.info(`USER JOINED ROOM: ${room}`);
socket.broadcast.to(room).emit("ready", socket.id);
});
socket.on("offer", (id, message) => {
socket.to(id).emit("offer", socket.id, message);
});
socket.on("answer", (id, message) => {
socket.to(id).emit("answer", socket.id, message);
});
socket.on("candidate", (id, message) => {
socket.to(id).emit("candidate", socket.id, message);
});
socket.on("disconnect", () => {
socket.broadcast.to(room).emit("bye", socket.id);
});
socket.join(room);
} else {
socket.emit("full", room);
}
});
};
/**
@param {SocketIO.Server} io
*/
module.exports = io => {
_io = io;
return { listen };
};