This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (55 loc) · 2.24 KB
/
index.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
'use strict';
let os = require('os');
let port = (process.env.APPLICATION_PORT) ? process.env.APPLICATION_PORT : 8080;
let fileServer = new(require('node-static').Server)();
let app = require('http').createServer(function(req, res) {
fileServer.serve(req, res);
}).listen(port);
let io = require('socket.io')(app);
io.on('connection', function(socket) {
// convenience function to log server messages on the client
function log() {
let array = ['Message from server:'];
array.push.apply(array, arguments);
socket.emit('log', array);
}
function RoomParticipants(room) {
return io.sockets.adapter.rooms[room] ? io.sockets.adapter.rooms[room].length : 0
}
socket.on('message', function(data, room) {
log('Client said: ', data);
// for a real app, would be room-only (not broadcast)
io.sockets.in(room).emit('message', data);
});
socket.on('ID of presenter', function(room, presenterID) {
io.sockets.in(room).emit('ID of presenter', presenterID);
});
socket.on('create or join', function(room) {
log('Received request to create or join room ' + room);
console.log("Number of all currently connected sockets: ",Object.keys(io.sockets.sockets).length);
log('Room ' + room + ' now has ' + RoomParticipants(room) + ' client(s)');
if (RoomParticipants(room) === 0) {
log('Client ID ' + socket.id + ' created room ' + room);
socket.join(room).emit('created', room, socket.id);
console.log("Number participants for room ", room,": ", RoomParticipants(room));
} else if (RoomParticipants(room) < 10) {
log('Client ID ' + socket.id + ' joined room ' + room);
io.sockets.in(room).emit('join', room, socket.id);
socket.join(room).emit('joined', room, socket.id);
io.sockets.in(room).emit('ready');
console.log("Number participants for room ", room,": ", RoomParticipants(room));
} else { // if room is full
socket.emit('full', room);
}
});
socket.on('ipaddr', function() {
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);
}
});
}
});
});