Commit 52b838b421ecc55fc1c778e1d1edbe395d56c854
1 parent
d9d4c20d43
Exists in
master
ping pong
Showing
1 changed file
with
17 additions
and
0 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 | 5 | ||
6 | const objWs = {}; | 6 | const objWs = {}; |
7 | 7 | ||
8 | objWs.wsServer = new webSocketServer({ | 8 | objWs.wsServer = new webSocketServer({ |
9 | port: config.port | 9 | port: config.port |
10 | }); | 10 | }); |
11 | 11 | ||
12 | function noop() {} | ||
13 | |||
14 | function heartbeat() { | ||
15 | this.isAlive = true; | ||
16 | } | ||
17 | |||
12 | objWs.wsServer.on('connection', function connection(ws) { | 18 | objWs.wsServer.on('connection', function connection(ws) { |
13 | 19 | ||
14 | console.log('open socket server'); | 20 | console.log('open socket server'); |
21 | ws.isAlive = true; | ||
22 | ws.on('pong', heartbeat); | ||
15 | 23 | ||
16 | ws.on('message', function incoming(message) { | 24 | ws.on('message', function incoming(message) { |
17 | 25 | ||
18 | message = JSON.parse(message.toString('utf8')); | 26 | message = JSON.parse(message.toString('utf8')); |
19 | 27 | ||
20 | switch (message.action) { | 28 | switch (message.action) { |
21 | case 'gln': | 29 | case 'gln': |
22 | clients.push({ws: ws, gln: message.gln}); | 30 | clients.push({ws: ws, gln: message.gln}); |
23 | break; | 31 | break; |
24 | default: | 32 | default: |
25 | // console.log(message.action); | 33 | // console.log(message.action); |
26 | } | 34 | } |
27 | }); | 35 | }); |
28 | }); | 36 | }); |
29 | 37 | ||
38 | const interval = setInterval(function ping() { | ||
39 | objWs.wsServer.clients.forEach(function each(ws) { | ||
40 | if (ws.isAlive === false) return ws.terminate(); | ||
41 | |||
42 | ws.isAlive = false; | ||
43 | ws.ping(noop); | ||
44 | }); | ||
45 | }, 10000); | ||
46 | |||
30 | objWs.getEntidad = function(gln, tableName, where, queryString) { | 47 | objWs.getEntidad = function(gln, tableName, where, queryString) { |
31 | 48 | ||
32 | return new Promise(function(resolve, reject) { | 49 | return new Promise(function(resolve, reject) { |
33 | 50 | ||
34 | var client = clients.filter(function(client) { | 51 | var client = clients.filter(function(client) { |
35 | return client.gln == gln | 52 | return client.gln == gln |
36 | })[0]; | 53 | })[0]; |
37 | 54 | ||
38 | if (!client) { | 55 | if (!client) { |
39 | reject('No se encuentra el webSocket client'); | 56 | reject('No se encuentra el webSocket client'); |
40 | } | 57 | } |
41 | 58 | ||
42 | var idSolicitud = Math.round(Math.random() * 1000); | 59 | var idSolicitud = Math.round(Math.random() * 1000); |
43 | 60 | ||
44 | var enviar = { | 61 | var enviar = { |
45 | action: 'getEntity', | 62 | action: 'getEntity', |
46 | idSolicitud: idSolicitud | 63 | idSolicitud: idSolicitud |
47 | }; | 64 | }; |
48 | 65 | ||
49 | if (queryString) { | 66 | if (queryString) { |
50 | 67 | ||
51 | enviar.queryString = queryString; | 68 | enviar.queryString = queryString; |
52 | } else { | 69 | } else { |
53 | enviar.tableName = tableName; | 70 | enviar.tableName = tableName; |
54 | enviar.where = where || {}; | 71 | enviar.where = where || {}; |
55 | } | 72 | } |
56 | 73 | ||
57 | client.ws.send(JSON.stringify(enviar)); | 74 | client.ws.send(JSON.stringify(enviar)); |
58 | 75 | ||
59 | client.ws.on('message', function(message) { | 76 | client.ws.on('message', function(message) { |
60 | 77 | ||
61 | message = JSON.parse(message); | 78 | message = JSON.parse(message); |
62 | 79 | ||
63 | if (idSolicitud == message.idSolicitud) { | 80 | if (idSolicitud == message.idSolicitud) { |
64 | 81 | ||
65 | resolve(message.data); | 82 | resolve(message.data); |
66 | } | 83 | } |
67 | 84 | ||
68 | }); | 85 | }); |
69 | }); | 86 | }); |
70 | } | 87 | } |
71 | 88 | ||
72 | objWs.getClientGln = function(gln) { | 89 | objWs.getClientGln = function(gln) { |
73 | return clients.filter(function(client) { | 90 | return clients.filter(function(client) { |
74 | return client.gln = gln; | 91 | return client.gln = gln; |
75 | }); | 92 | }); |
76 | } | 93 | } |
77 | 94 | ||
78 | objWs.guardarComprobante = function (cuerpo, gln) { | 95 | objWs.guardarComprobante = function (cuerpo, gln) { |
79 | 96 | ||
80 | return new Promise((resolve, reject) => { | 97 | return new Promise((resolve, reject) => { |
81 | 98 | ||
82 | var client = clients.filter(function(client) { | 99 | var client = clients.filter(function(client) { |
83 | return client.gln == gln | 100 | return client.gln == gln |
84 | })[0]; | 101 | })[0]; |
85 | 102 | ||
86 | if (!client) { | 103 | if (!client) { |
87 | reject('No se encuentra el webSocket client'); | 104 | reject('No se encuentra el webSocket client'); |
88 | console.log('No se encuentra el webSocket client'); | 105 | console.log('No se encuentra el webSocket client'); |
89 | return; | 106 | return; |
90 | } | 107 | } |
91 | 108 | ||
92 | var idSolicitud = Math.round(Math.random() * 1000); | 109 | var idSolicitud = Math.round(Math.random() * 1000); |
93 | 110 | ||
94 | var enviar = { | 111 | var enviar = { |
95 | action: 'comprobante', | 112 | action: 'comprobante', |
96 | req: cuerpo, | 113 | req: cuerpo, |
97 | idSolicitud: idSolicitud | 114 | idSolicitud: idSolicitud |
98 | }; | 115 | }; |
99 | 116 | ||
100 | console.log(`enviando comprobante a cliente`) | 117 | console.log(`enviando comprobante a cliente`) |
101 | console.log(cuerpo.cabecera); | 118 | console.log(cuerpo.cabecera); |
102 | 119 | ||
103 | client.ws.send(JSON.stringify(enviar)); | 120 | client.ws.send(JSON.stringify(enviar)); |
104 | 121 | ||
105 | client.ws.on('message', function(message) { | 122 | client.ws.on('message', function(message) { |
106 | 123 | ||
107 | message = JSON.parse(message); | 124 | message = JSON.parse(message); |
108 | console.log(message); | 125 | console.log(message); |
109 | if (idSolicitud == message.idSolicitud) { | 126 | if (idSolicitud == message.idSolicitud) { |
110 | 127 | ||
111 | if (message.ok) { | 128 | if (message.ok) { |
112 | console.log('resuelve comprobante en estación') | 129 | console.log('resuelve comprobante en estación') |
113 | resolve(); | 130 | resolve(); |
114 | } else { | 131 | } else { |
115 | 132 | ||
116 | reject(); | 133 | reject(); |
117 | } | 134 | } |
118 | } | 135 | } |
119 | 136 | ||
120 | }); | 137 | }); |
121 | 138 | ||
122 | }); | 139 | }); |
123 | } | 140 | } |
124 | 141 | ||
125 | return objWs; | 142 | return objWs; |
126 | } | 143 | } |
127 | 144 |