index.js
1.37 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
module.exports = function() {
const webSocketServer = require('ws').Server;
var clients = [];
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, queryString) {
return new Promise(function(resolve, reject) {
var client = clients.filter(function(client) {
return client.gln == gln
})[0];
if (!client) {
reject('No se encuentra el webSocket client');
}
var idSolicitud = Math.round(Math.random() * 1000);
var enviar = {
action: 'getEntity',
idSolicitud: idSolicitud
};
if (queryString) {
enviar.queryString = queryString;
} else {
enviar.tableName = tableName;
enviar.where = where || {};
}
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;
}