Commit 9ee548483b546d6037f1fd1c755c0c3aa7794e24
1 parent
55c7bcadb1
Exists in
master
linea al final
Showing
1 changed file
with
1 additions
and
1 deletions
Show diff stats
webSocketServer/index.js
1 | module.exports = function() { | 1 | module.exports = function() { |
2 | 2 | ||
3 | const webSocketServer = require('ws').Server; | 3 | const webSocketServer = require('ws').Server; |
4 | var clients = []; | 4 | var clients = []; |
5 | var idsSolicitud = []; | 5 | var idsSolicitud = []; |
6 | 6 | ||
7 | const objWs = {}; | 7 | const objWs = {}; |
8 | 8 | ||
9 | objWs.wsServer = new webSocketServer({ | 9 | objWs.wsServer = new webSocketServer({ |
10 | port: config.port | 10 | port: config.port |
11 | }); | 11 | }); |
12 | 12 | ||
13 | objWs.wsServer.on('connection', function connection(ws) { | 13 | objWs.wsServer.on('connection', function connection(ws) { |
14 | 14 | ||
15 | console.log('open socket server'); | 15 | console.log('open socket server'); |
16 | 16 | ||
17 | ws.on('message', function incoming(message) { | 17 | ws.on('message', function incoming(message) { |
18 | 18 | ||
19 | message = JSON.parse(message.toString('utf8')); | 19 | message = JSON.parse(message.toString('utf8')); |
20 | 20 | ||
21 | switch (message.action) { | 21 | switch (message.action) { |
22 | case 'gln': | 22 | case 'gln': |
23 | clients.push({ws: ws, gln: message.gln}); | 23 | clients.push({ws: ws, gln: message.gln}); |
24 | break; | 24 | break; |
25 | default: | 25 | default: |
26 | // console.log(message.action); | 26 | // console.log(message.action); |
27 | } | 27 | } |
28 | }); | 28 | }); |
29 | }); | 29 | }); |
30 | 30 | ||
31 | objWs.getEntidad = function(gln, tableName, where) { | 31 | objWs.getEntidad = function(gln, tableName, where) { |
32 | 32 | ||
33 | return new Promise(function(resolve, reject) { | 33 | return new Promise(function(resolve, reject) { |
34 | 34 | ||
35 | var client = clients.filter(function(client) { | 35 | var client = clients.filter(function(client) { |
36 | return client.gln == gln | 36 | return client.gln == gln |
37 | })[0]; | 37 | })[0]; |
38 | 38 | ||
39 | var idSolicitud = Math.round(Math.random() * 1000); | 39 | var idSolicitud = Math.round(Math.random() * 1000); |
40 | 40 | ||
41 | var enviar = { | 41 | var enviar = { |
42 | action: 'getEntity', | 42 | action: 'getEntity', |
43 | tableName: tableName, | 43 | tableName: tableName, |
44 | where: where || {}, | 44 | where: where || {}, |
45 | idSolicitud: idSolicitud | 45 | idSolicitud: idSolicitud |
46 | } | 46 | } |
47 | 47 | ||
48 | idsSolicitud.push(idSolicitud); | 48 | idsSolicitud.push(idSolicitud); |
49 | 49 | ||
50 | client.ws.send(JSON.stringify(enviar)); | 50 | client.ws.send(JSON.stringify(enviar)); |
51 | 51 | ||
52 | client.ws.on('message', function(message) { | 52 | client.ws.on('message', function(message) { |
53 | 53 | ||
54 | message = JSON.parse(message); | 54 | message = JSON.parse(message); |
55 | 55 | ||
56 | if (idSolicitud == message.idSolicitud) { | 56 | if (idSolicitud == message.idSolicitud) { |
57 | 57 | ||
58 | resolve(message.data); | 58 | resolve(message.data); |
59 | } | 59 | } |
60 | 60 | ||
61 | }); | 61 | }); |
62 | }); | 62 | }); |
63 | } | 63 | } |
64 | 64 | ||
65 | return objWs; | 65 | return objWs; |
66 | } | ||
66 | } |