-
Notifications
You must be signed in to change notification settings - Fork 4
/
local.js
91 lines (73 loc) · 2.71 KB
/
local.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
80
81
82
83
84
85
86
87
88
89
90
91
'use strict'
var express = require('express');
var app = express();
var server = require('http').createServer(app).listen(8080);
var io = require('socket.io').listen(server);
var path = require('path');
var connections = [];
var credentials = {
'iceServers': [{
'urls': 'stun:stun.l.google.com:19302'
}]
};
// The default namespace is by default '/', but this variable is to use with numClientsInRoom
var defaultNamespace = '/';
console.log('Server running at port ' + '8080');
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html')
});
app.use('/js', express.static(path.join(__dirname, '/js')));
app.use('/styles', express.static(path.join(__dirname, '/styles')));
app.use('/media', express.static(path.join(__dirname, '/media')));
io.sockets.on('connection', function(socket) {
connections.push(socket);
console.log('Connected: %s sockets connected', connections.length);
// Convenience function to log server messages on the client (client listens to it on socket.on('log'))
function log() {
var array = ['Message from server: '];
array.push.apply(array, arguments);
socket.emit('log', array);
}
socket.emit('credentials', credentials);
socket.on('message', function(message, room) {
log('Client said: ', message);
socket.broadcast.to(room).emit('message', message);
});
socket.on('disconnect', function(data) {
connections.splice(connections.indexOf(socket), 1);
console.log('Disconnected: %s sockets connected', connections.length);
});
socket.on('create or join', function(room) {
// Total number of clients in the socket
log('Received request to create or join room ' + room);
var numClients = numClientsInRoom(defaultNamespace, room);
console.log(numClients);
if(numClients === 0) {
socket.join(room);
console.log(io.nsps[defaultNamespace].adapter.rooms[room].length);
log('Client ID ' + socket.id + ' created room ' + room);
socket.emit('created', room, socket.id);
} else if(numClients === 1) {
log('Client ID ' + socket.id + ' joined room ' + room);
socket.join(room);
socket.emit('joined', room, socket.id);
console.log(io.nsps[defaultNamespace].adapter.rooms[room].length);
io.sockets.in(room).emit('ready');
} else {
// Max two clients for now
socket.emit('full', room);
}
});
});
/* Function to find out how many clients there are in a room
Used to minimize each room to contain x clients */
function numClientsInRoom(namespace, room) {
if(io.nsps[namespace].adapter.rooms[room] === undefined){
console.log('Make room');
return 0;
}
else{
console.log('Join room: ', room);
return io.nsps[namespace].adapter.rooms[room].length;
}
}