-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·102 lines (101 loc) · 3.13 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
var logger = require('./logger').logger(__filename);
var redis = require('./redis');
var port = process.argv[2] || 8001,
idProcess = process.argv[3] || 8000;
var io = require('socket.io')(port, '0.0.0.0');
var devices = [];
var ind, ld;
process.setMaxListeners(10000);
redis.init(function (err) {
if (err) logger.error(err);
});
var redisAdapter = require('socket.io-redis');
io.adapter(redisAdapter({host: 'localhost', port: 6379}));
logger.debug('[' + idProcess + '] server at port ' + port);
io.sockets.on('connection', function (socket) {
socket.on('newMessage', function (data) {
switch (data.event) {
case 0:
var found = false;
ld = devices.length;
for (ind = 0; ind < ld; ind++) {
if (socket === devices[ind].socket) {
devices[ind].socket.emit('subscription confirm', {data: 0, socket: socket.id});
found = true;
break;
}
}
if (!found) {
socket.emit('subscription confirm', {data: 1, socket: socket.id});
devices.push({socket: socket});
var obj = JSON.stringify({numClients: devices.length, numPid: process.pid, port: port});
redis.set(idProcess, obj, function (err, result) {
if (err) logger.error(err);
else {
logger.debug('update to redis, key:' + idProcess + ' new value:' + obj);
process.send({numClients: devices.length, numPid: process.pid, port: port});
}
});
}
break;
default:
logger.info('event not recognized');
break;
}
});
socket.on('disconnect', function () {
logger.info('disconnect socket received - socket:' + socket.id);
deleteSocketDevice(socket);
process.send({numClients: devices.length, numPid: process.pid, port: port});
});
socket.on('error', function () {
logger.info('server error - socket:' + message.id);
deleteSocketDevice(socket);
process.send({numClients: devices.length, numPid: process.pid, port: port});
});
function deleteSocketDevice(id) {
for (var i = 0; i < devices.length; i++) {
if (devices[i].socket == id) {
logger.info('socket deleted - socket:' + id.id);
devices.splice(i, 1);
redisApply(idProcess, JSON.stringify({
numClients: devices.length,
numPid: process.pid,
port: port
}));
return (true);
}
}
return false;
}
process.on('message', msgFromController);
function redisApply(idProcess, json) {
redis.set(idProcess, json, function (err, result) {
if (err) logger.error(err);
else {
logger.debug('update to redis (decrement socket), key:' + idProcess);
}
});
}
function msgFromController(m) {
switch (m.code) {
case 1:
for (var i = 0; i < devices.length; i++) {
if (socket === devices[i].socket) {
devices[i].socket.emit('msgFromServer', {data: m});
}
}
break;
case 2:
for (var ii = 0; ii < devices.length; ii++) {
if (socket === devices[ii].socket) {
if (devices[ii].socket.id == m.socketId) {
devices[ii].socket.emit('msgFromServer', {data: m});
}
}
}
break;
}
process.send({numClients: devices.length, numPid: process.pid, port: port});
}
});