Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
70 lines (65 loc) · 2.12 KB
/
Copy pathserver.js
File metadata and controls
70 lines (65 loc) · 2.12 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
var WebSocket = new require('ws');
const queryString = require('query-string');
var webSocketServer = new WebSocket.Server({
port: 9001
});
var clients = {};
webSocketServer.on('connection', function (ws, incoming_request) {
const params = queryString.parse(incoming_request.url);
const machine_id = params['/?machine_id'];
const client_id = params['client_id'];
clients[client_id] = { socket: ws, timer: null };
console.log("New connetion! Machine_id=" + machine_id + " Client_id=" + client_id);
ws.on('message', function (message) {
try {
var mess_obj = JSON.parse(message);
console.log('New messange ' + mess_obj.action);
switch (mess_obj.action) {
case "start":
clients[client_id].timer = setInterval(SendToDashboard, 500, client_id);
break;
default:
break;
}
}
catch {
console.log('New messange ' + message);
}
});
ws.on('close', function () {
if (clients[client_id] !== undefined) {
if (clients[client_id].timer !== undefined | clients[client_id].timer !== null) clearInterval(clients[client_id].timer);
if (clients[client_id] !== null) delete clients[client_id];
}
console.log('Connection closed ' + client_id);
});
});
function SendToDashboard(client_id) {
if(clients[client_id] !== undefined) clients[client_id].socket.send(JSON.stringify({
machine:
[
{
title: '1111',
params: [
{ one: Math.random() }
]
},
{
title: '2222',
params: [
{ one: Math.random() },
{ two: Math.random() },
{ three: Math.random() }
]
},
{
title: '3333',
params: [
{ one: Math.random() },
{ two: Math.random() }
]
}
]
}));
return true;
}