@arcblock/ws 1.18.73 → 1.18.75
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/server/index.js +38 -19
- 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,
|
16
|
+
const reply = ({ socket, topic, event, data = {}, status = 'ok', ref = '', joinRef = '' }) => {
|
17
17
|
if (socket.readyState === WebSocket.OPEN) {
|
18
|
-
const res = JSON.stringify([
|
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,7 @@ class WsServer extends EventEmitter {
|
|
325
328
|
}
|
326
329
|
this.topics[topic].add(socket);
|
327
330
|
|
328
|
-
reply(socket, topic, `chan_reply_${ref}
|
331
|
+
reply({ socket, topic, event: `chan_reply_${ref}`, ref, joinRef });
|
329
332
|
this.emit('channel.join', { socket, topic, event, payload });
|
330
333
|
|
331
334
|
// post hook
|
@@ -348,13 +351,21 @@ class WsServer extends EventEmitter {
|
|
348
351
|
if (!this.skipLogOnHookError) {
|
349
352
|
this.logger.error('preLeaveChannel error', { error });
|
350
353
|
}
|
351
|
-
reply(
|
354
|
+
reply({
|
355
|
+
socket,
|
356
|
+
topic,
|
357
|
+
event: `chan_reply_${ref}`,
|
358
|
+
data: { message: error.message },
|
359
|
+
status: 'error',
|
360
|
+
ref,
|
361
|
+
joinRef,
|
362
|
+
});
|
352
363
|
return;
|
353
364
|
}
|
354
365
|
|
355
366
|
// leave
|
356
367
|
this._leaveChannel(socket, topic);
|
357
|
-
reply(socket, topic, `chan_reply_${ref}
|
368
|
+
reply({ socket, topic, event: `chan_reply_${ref}`, ref, joinRef });
|
358
369
|
|
359
370
|
// post hook
|
360
371
|
try {
|
@@ -375,11 +386,19 @@ class WsServer extends EventEmitter {
|
|
375
386
|
if (!this.skipLogOnHookError) {
|
376
387
|
this.logger.error('receiveMessage error', { error });
|
377
388
|
}
|
378
|
-
reply(
|
389
|
+
reply({
|
390
|
+
socket,
|
391
|
+
topic,
|
392
|
+
event: `chan_reply_${ref}`,
|
393
|
+
data: { message: error.message },
|
394
|
+
status: 'error',
|
395
|
+
ref,
|
396
|
+
joinRef,
|
397
|
+
});
|
379
398
|
return;
|
380
399
|
}
|
381
400
|
|
382
|
-
reply(socket, topic, `chan_reply_${ref}`,
|
401
|
+
reply({ socket, topic, event: `chan_reply_${ref}`, ref, joinRef });
|
383
402
|
});
|
384
403
|
|
385
404
|
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.75",
|
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.75",
|
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": "21d91fc7d8ef1bea68846ace369f00e294dec6fe"
|
49
49
|
}
|