-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
88 lines (72 loc) · 2.26 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
80
81
82
83
84
85
86
87
88
var app, express, io, server, uuid, ws, appPort;
express = require('express');
app = express();
ws = require('websocket.io');
uuid = require('node-uuid');
port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
app.configure(function () {
app.use(express.static(__dirname + '/page'));
});
app.set('port', process.env.PORT || 3000);
app.get('/:group', function(req, res) { //
var _ref;
return res.render('index.jade', {
params: req.query,
groupCount: ((_ref = io.clientsByGroup[req.params.group]) != null ? _ref.length : void 0) || 0
});
});
server = app.listen(port, ip);
io = ws.attach(server);
io.clientsById || (io.clientsById = {});
io.clientsByGroup || (io.clientsByGroup = {});
io.on('connection', function(socket) {
var group, _base;
group = (socket.req.url !="/") ? /\/(.+)/.exec(socket.req.url)[1] : ":base";
socket.id = uuid.v1();
socket.group = group;
if (!group) {
socket.close();
return;
}
(_base = io.clientsByGroup)[group] || (_base[group] = []);
io.clientsByGroup[group].push(socket);
io.clientsById[socket.id] = socket;
socket.send(
JSON.stringify({
type: 'uuid',
id: socket.id
})
);
socket.send(
JSON.stringify({
type: 'node-debug',
key: 'group',
value: group
})
);
return socket.on('message', function(data) {
var msg, sock, i, length, _ref, results;
msg = JSON.parse(data);
switch (msg.type) {
case 'offered':
case 'candidate':
case 'answered':
_ref = io.clientsByGroup[socket.group];
results = [];
for (i = 0, length = _ref.length; i < length; i++) {
sock = _ref[i];
if (sock.id !== socket.id) {
results.push(sock.send(JSON.stringify(msg)));
} else {
results.push(void 0);
}
}
return results;
break;
case 'close':
return socket.close();
}
});
});
module.exports = app;