@arcblock/ws 1.18.73 → 1.18.74
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/server/index.js +58 -20
- package/package.json +4 -4
package/lib/server/index.js
CHANGED
@@ -13,9 +13,9 @@ const sleep = (timeout) =>
|
|
13
13
|
setTimeout(resolve, timeout);
|
14
14
|
});
|
15
15
|
|
16
|
-
const reply = (socket, topic, event,
|
17
|
-
if (socket.readyState === WebSocket.OPEN) {
|
18
|
-
const res = JSON.stringify([
|
16
|
+
const reply = ({ socket, topic, event, data, status = 'ok', ref = '', joinRef = '' }) => {
|
17
|
+
if (socket.readyState === WebSocket.OPEN && socket.channel[topic]) {
|
18
|
+
const res = JSON.stringify([joinRef, ref, topic, event, { status, response: data }]);
|
19
19
|
socket.send(res);
|
20
20
|
}
|
21
21
|
};
|
@@ -199,7 +199,7 @@ class WsServer extends EventEmitter {
|
|
199
199
|
if (enableLog) {
|
200
200
|
this.logger.info('broadcast message to', { topic, event, id: socket.id });
|
201
201
|
}
|
202
|
-
reply(socket, topic, event, data);
|
202
|
+
reply({ socket, topic, event, data });
|
203
203
|
});
|
204
204
|
}
|
205
205
|
|
@@ -253,7 +253,7 @@ class WsServer extends EventEmitter {
|
|
253
253
|
this.logger.info('send message to', { topic, event, id: socket.id });
|
254
254
|
}
|
255
255
|
|
256
|
-
reply(socket, topic, event, data);
|
256
|
+
reply({ socket, topic, event, data });
|
257
257
|
|
258
258
|
try {
|
259
259
|
await this.hooks.postSend({ topic, event, data, options });
|
@@ -269,13 +269,13 @@ class WsServer extends EventEmitter {
|
|
269
269
|
*/
|
270
270
|
async onWssConnection(socket) {
|
271
271
|
socket.id = nanoid();
|
272
|
-
socket.channel = {};
|
272
|
+
socket.channel = {}; // This should be renamed to channels
|
273
273
|
|
274
274
|
refreshHeartbeat(socket);
|
275
275
|
this.logger.debug('socket connected', { id: socket.id });
|
276
276
|
|
277
277
|
socket.on('message', async (msg) => {
|
278
|
-
this.logger.debug('socket onmessage',
|
278
|
+
this.logger.debug('socket onmessage', msg.toString());
|
279
279
|
let joinRef;
|
280
280
|
let ref;
|
281
281
|
let topic;
|
@@ -293,12 +293,9 @@ class WsServer extends EventEmitter {
|
|
293
293
|
return;
|
294
294
|
}
|
295
295
|
|
296
|
-
socket.joinRef = joinRef;
|
297
|
-
socket.ref = ref;
|
298
|
-
|
299
296
|
if (topic === 'phoenix' && event === 'heartbeat') {
|
300
297
|
// heartbeat
|
301
|
-
reply(socket, topic, event);
|
298
|
+
reply({ socket, topic, event, ref });
|
302
299
|
refreshHeartbeat(socket);
|
303
300
|
return;
|
304
301
|
}
|
@@ -308,14 +305,20 @@ class WsServer extends EventEmitter {
|
|
308
305
|
try {
|
309
306
|
const authInfo = await this.hooks.authenticateJoinChannel({ socket, joinRef, ref, topic, event, payload });
|
310
307
|
await this.hooks.preJoinChannel({ socket, joinRef, ref, topic, event, payload });
|
311
|
-
|
312
|
-
socket.channel[topic] = {};
|
313
|
-
socket.channel[topic].authInfo = authInfo;
|
308
|
+
socket.channel[topic] = { authInfo };
|
314
309
|
} catch (error) {
|
315
310
|
if (!this.skipLogOnHookError) {
|
316
311
|
this.logger.error('preJoinChannel error', { error });
|
317
312
|
}
|
318
|
-
reply(
|
313
|
+
reply({
|
314
|
+
socket,
|
315
|
+
topic,
|
316
|
+
event: `chan_reply_${ref}`,
|
317
|
+
data: { message: error.message },
|
318
|
+
status: 'error',
|
319
|
+
ref,
|
320
|
+
joinRef,
|
321
|
+
});
|
319
322
|
return;
|
320
323
|
}
|
321
324
|
|
@@ -325,7 +328,13 @@ class WsServer extends EventEmitter {
|
|
325
328
|
}
|
326
329
|
this.topics[topic].add(socket);
|
327
330
|
|
328
|
-
reply(
|
331
|
+
reply({
|
332
|
+
socket,
|
333
|
+
topic,
|
334
|
+
event: `chan_reply_${ref}`,
|
335
|
+
ref,
|
336
|
+
joinRef,
|
337
|
+
});
|
329
338
|
this.emit('channel.join', { socket, topic, event, payload });
|
330
339
|
|
331
340
|
// post hook
|
@@ -348,13 +357,27 @@ class WsServer extends EventEmitter {
|
|
348
357
|
if (!this.skipLogOnHookError) {
|
349
358
|
this.logger.error('preLeaveChannel error', { error });
|
350
359
|
}
|
351
|
-
reply(
|
360
|
+
reply({
|
361
|
+
socket,
|
362
|
+
topic,
|
363
|
+
event: `chan_reply_${ref}`,
|
364
|
+
data: { message: error.message },
|
365
|
+
status: 'error',
|
366
|
+
ref,
|
367
|
+
joinRef,
|
368
|
+
});
|
352
369
|
return;
|
353
370
|
}
|
354
371
|
|
355
372
|
// leave
|
356
373
|
this._leaveChannel(socket, topic);
|
357
|
-
reply(
|
374
|
+
reply({
|
375
|
+
socket,
|
376
|
+
topic,
|
377
|
+
event: `chan_reply_${ref}`,
|
378
|
+
ref,
|
379
|
+
joinRef,
|
380
|
+
});
|
358
381
|
|
359
382
|
// post hook
|
360
383
|
try {
|
@@ -375,11 +398,26 @@ class WsServer extends EventEmitter {
|
|
375
398
|
if (!this.skipLogOnHookError) {
|
376
399
|
this.logger.error('receiveMessage error', { error });
|
377
400
|
}
|
378
|
-
reply(
|
401
|
+
reply({
|
402
|
+
socket,
|
403
|
+
topic,
|
404
|
+
event: `chan_reply_${ref}`,
|
405
|
+
data: { message: error.message },
|
406
|
+
status: 'error',
|
407
|
+
ref,
|
408
|
+
joinRef,
|
409
|
+
});
|
379
410
|
return;
|
380
411
|
}
|
381
412
|
|
382
|
-
reply(
|
413
|
+
reply({
|
414
|
+
socket,
|
415
|
+
topic,
|
416
|
+
event: `chan_reply_${ref}`,
|
417
|
+
data: {},
|
418
|
+
ref,
|
419
|
+
joinRef,
|
420
|
+
});
|
383
421
|
});
|
384
422
|
|
385
423
|
socket.on('close', () => {
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@arcblock/ws",
|
3
|
-
"version": "1.18.
|
3
|
+
"version": "1.18.74",
|
4
4
|
"description": "OCAP Chain websocket server and client",
|
5
5
|
"keywords": [
|
6
6
|
"websocket"
|
@@ -34,16 +34,16 @@
|
|
34
34
|
"url": "https://github.com/ArcBlock/asset-chain/issues"
|
35
35
|
},
|
36
36
|
"dependencies": {
|
37
|
-
"@arcblock/event-hub": "1.18.
|
37
|
+
"@arcblock/event-hub": "1.18.74",
|
38
38
|
"debug": "^4.3.4",
|
39
39
|
"eventemitter3": "^4.0.7",
|
40
40
|
"lodash": "^4.17.21",
|
41
41
|
"nanoid": "~3.3.4",
|
42
|
-
"phoenix": "1.
|
42
|
+
"phoenix": "1.7.2",
|
43
43
|
"ws": "^8.9.0"
|
44
44
|
},
|
45
45
|
"devDependencies": {
|
46
46
|
"get-port": "^5.1.1"
|
47
47
|
},
|
48
|
-
"gitHead": "
|
48
|
+
"gitHead": "87ab9471b0de6669100b56138193ec1705a97fb2"
|
49
49
|
}
|