-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.js
65 lines (52 loc) · 1.21 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
var http = require('http')
var ecstatic = require('ecstatic');
var fs = require('fs');
var Engine = require('engine.io-stream');
var httpServer = http.createServer(ecstatic({
root: __dirname
})).listen(8080);
console.log("listening on port 8080");
var clients = [];
var userTable = {};
/*
* Video WebSocket
*/
var vidEngine = Engine(function (socket) {
socket.once("data", function (data) {
console.log("connection made!", data);
clients.push({
id: data,
socket: socket
});
})
socket.on('data', function (data) {
var obj = {};
for (var i=0; i < clients.length; i++) {
obj[clients[i].id] = data;
clients[i].socket.write(JSON.stringify(obj));
}
});
socket.on('end', function () {
var i = clients.indexOf(clients);
clients.splice(i, 1);
});
});
/*
* Room WebSockets
*/
var roomEngine = Engine(function (socket) {
console.log("socket user");
socket.on('data', function (data) {
var d = JSON.parse(data);
if (!(d.id in userTable)) {
userTable[d.id] = {
username: d.username,
createdAt: new Date()
};
}
console.log(JSON.stringify(userTable));
socket.write(JSON.stringify(userTable));
})
});
vidEngine.attach(httpServer, "/video");
roomEngine.attach(httpServer, "/room");