index.js
1.24 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
module.exports = function() {
const webSocketServer = require('ws').Server;
var clients = [];
var idsSolicitud = [];
const objWs = {};
objWs.wsServer = new webSocketServer({
port: config.port
});
objWs.wsServer.on('connection', function connection(ws) {
console.log('open socket server');
ws.on('message', function incoming(message) {
message = JSON.parse(message.toString('utf8'));
switch (message.action) {
case 'gln':
clients.push({ws: ws, gln: message.gln});
break;
default:
// console.log(message.action);
}
});
});
objWs.getEntidad = function(gln, tableName, where) {
return new Promise(function(resolve, reject) {
var client = clients.filter(function(client) {
return client.gln == gln
})[0];
var idSolicitud = Math.round(Math.random() * 1000);
var enviar = {
action: 'getEntity',
tableName: tableName,
where: where || {},
idSolicitud: idSolicitud
}
idsSolicitud.push(idSolicitud);
client.ws.send(JSON.stringify(enviar));
client.ws.on('message', function(message) {
message = JSON.parse(message);
if (idSolicitud == message.idSolicitud) {
resolve(message.data);
}
});
});
}
return objWs;
}