-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
46 lines (39 loc) · 1.14 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
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({ port: 8088 });
var displays = [];
var sensors = [];
var idCounter = 0;
wss.on('connection', function connection(ws) {
//console.log('a client connected!');
ws.id = idCounter;
idCounter++;
ws.on('message', function incoming(msg) {
//console.log("msg: ", msg);
msg = JSON.parse(msg);
//console.log("msg:", msg);
if (typeof ws.type === "undefined") {
if (msg.type === "sensor") {
console.log('a sensor connected');
ws.type = "sensor";
} else if (msg.type === "display") {
console.log('a display connected');
ws.type = "display";
} else {
console.log('an unknow device connected');
}
}
if (ws.type === "sensor" && msg.type === "sensor") {
wss.clients.forEach(function each(client) {
if (client.type === "display"){
client.send(JSON.stringify({
type: 'sensor',
sensor: msg.sensor,
id: ws.id
}));
}
});
}
});
// say hello to our little friend
ws.send(JSON.stringify({type: "hello", id: ws.id}));
});