-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
79 lines (62 loc) · 2.39 KB
/
server.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';
const ADDRESS = '0.0.0.0';
const PORT = 8080;
const MAX_CLIENTS = 50;
let os = require('os');
let app = require('http').createServer(handler);
let io = require('socket.io')(app);
app.listen(PORT, ADDRESS);
console.log(`Socket.io server listening on ${ADDRESS}:${PORT}...`);
// This response can be used to debug firewall or other connectivity issues
function handler (req, res) {
res.statusCode = 404;
res.write('<h1>404 - Not Found</h1>');
res.end();
}
io.on('connection', (socket) => {
socket.on('join', (room) => {
console.log('Received request to create or join room ' + room);
let clientsInRoom = io.sockets.adapter.rooms[room];
let numClients = clientsInRoom ? Object.keys(clientsInRoom.sockets).length : 0;
// Add 1 since we'll be adding ourselves shortly
numClients += 1;
console.log(`Room ${room} now has ${numClients} client(s)`);
if (numClients === 1) {
socket.join(room);
console.log(`Client ID ${socket.id} created room ${room}`);
socket.emit('created', room, socket.id);
} else if (numClients < MAX_CLIENTS) {
socket.join(room);
console.log(`Client ID ${socket.id} joined room ${room}`);
socket.emit('joined', room, socket.id);
io.to(room).emit('join', socket.id);
} else {
console.log(`Max clients (${MAX_CLIENT}) reached.`);
socket.emit('full', room);
}
});
socket.on('offer', (offer, recipientId) => {
io.to(recipientId).emit('offer', offer, socket.id);
});
socket.on('answer', (answer, recipientId) => {
io.to(recipientId).emit('answer', answer, socket.id);
});
socket.on('candidate', (candidate, recipientId) => {
io.to(recipientId).emit('candidate', candidate, socket.id);
});
socket.on('leave', (room, socketId) => {
io.to(room).emit('leave', room, socketId);
socket.leave(room);
console.log(`Client ID ${socket.id} left room ${room}`);
});
socket.on('ipaddr', () => {
let ifaces = os.networkInterfaces();
for (let dev in ifaces) {
ifaces[dev].forEach(function (details) {
if (details.family === 'IPv4' && details.address !== '127.0.0.1') {
socket.emit('ipaddr', details.address);
}
});
}
});
});