/
RoomService.js
43 lines (39 loc) · 1.1 KB
/
RoomService.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
41
42
43
/** @type {SocketIO.Server} */
let _io;
const MAX_CLIENTS = 3;
/** @param {SocketIO.Socket} socket */
function listen(socket) {
const io = _io;
const rooms = io.nsps['/'].adapter.rooms;
socket.on('join', function(room) {
let numClients = 0;
if (rooms[room]) {
numClients = rooms[room].length;
}
if (numClients < MAX_CLIENTS) {
socket.on('ready', function() {
socket.broadcast.to(room).emit('ready', socket.id);
});
socket.on('offer', function (id, message) {
socket.to(id).emit('offer', socket.id, message);
});
socket.on('answer', function (id, message) {
socket.to(id).emit('answer', socket.id, message);
});
socket.on('candidate', function (id, message) {
socket.to(id).emit('candidate', socket.id, message);
});
socket.on('disconnect', function() {
socket.broadcast.to(room).emit('bye', socket.id);
});
socket.join(room);
} else {
socket.emit('full', room);
}
});
}
/** @param {SocketIO.Server} io */
module.exports = function(io) {
_io = io;
return {listen};
};