-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathserver.js
More file actions
85 lines (72 loc) · 1.96 KB
/
server.js
File metadata and controls
85 lines (72 loc) · 1.96 KB
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
var app = require('http').createServer(handler).listen(8080)
, fs = require('fs')
, io = require('socket.io').listen(app);
io.set('log level', 1);
var muralSockets = [];
var motionEvents = [];
var emitRate = 200;
function handler (req, res) {
if(req.url === "/"){
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200)
res.end(data);
});
}
else{
fs.readFile(__dirname + req.url, function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading ' +req.url);
}
res.writeHead(200);
res.end(data);
});
}
}
//add list of events
//push events every x seconds
//add to list on events
/*
setInterval(function(){
if(motionEvents.length>0){
io.sockets.in('muralRoom').emit('mural',{data:motionEvents});
}
motionEvents = [];
},emitRate);
*/
io.sockets.on('connection', function(socket){
console.log('connected on ' + socket.id);
socket.on('identify', function(data){
muralSockets.push(socket.id);
socket.join('muralRoom');
console.log("mural connected on " + socket.id);
});
socket.on('clear', function(data){
muralSockets.push(socket.id);
socket.broadcast.to('muralRoom').emit('clear', {});
});
socket.on('motion',function(data){
data.id = socket.id;
socket.broadcast.to('muralRoom').emit('mural', {data:[data]});
});
socket.on('disconnect', function() {
console.log('Got disconnect!');
var index = muralSockets.indexOf(socket.id);
if (index > -1) {
muralSockets.splice(index, 1);
socket.leave('muralRoom');
console.log("mural disconnected on " + socket.id);
}
else {
if(muralSockets.length>0){
console.log("phone disconnected on " + socket.id);
io.sockets.in('muralRoom').emit('removeCell',{data:socket.id});
}
}
});
});