@arcblock/ws 1.18.45 → 1.18.47

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -40,8 +40,9 @@ const wsServer = new WsServer({
40
40
  wsSerer.attach();
41
41
 
42
42
  // push message
43
- wsServer.push('blocklet.installed', data);
44
- wsServer.push('notification.create', data);
43
+ wsServer.broadcast('blocklet.installed', data);
44
+ wsServer.broadcast('notification.create', data);
45
+ wsServer.broadcast('topic', 'event', data);
45
46
  ```
46
47
 
47
48
  2. WsClient
@@ -74,6 +75,12 @@ socket.on('notification.create', callback2);
74
75
  socket.off('blocklet.installed', callback1);
75
76
  socket.off('notification.create', callback2);
76
77
 
78
+ // More flexible subscription
79
+ const subscription = socket.subscribe('topic', {});
80
+ subscription.on('event', ({ response }) => {
81
+ // Do something with the event data
82
+ });
83
+
77
84
  // disconnect
78
85
  socket.disconnect(() => {
79
86
  // after disconnected...
@@ -18,12 +18,12 @@ module.exports = (Socket, EventEmitter, transport) => {
18
18
  this._logger.error('socket error', err.error);
19
19
  });
20
20
  this.onMessage((message) => {
21
- this._logger.debug('socket message', { message });
21
+ this._logger.debug('socket message', message);
22
22
  });
23
23
  }
24
24
 
25
- on(event, handler) {
26
- this.ensureJoinChannel(event);
25
+ on(event, handler, params = {}) {
26
+ this.ensureJoinChannel(event, params);
27
27
  this.emitter.on(event, handler);
28
28
  }
29
29
 
@@ -47,30 +47,29 @@ module.exports = (Socket, EventEmitter, transport) => {
47
47
  /**
48
48
  * private
49
49
  */
50
- ensureJoinChannel(event) {
51
- const count = this.emitter.listenerCount(event);
50
+ ensureJoinChannel(topic, params = {}) {
51
+ const count = this.emitter.listenerCount(topic);
52
52
  if (count > 0) {
53
53
  return;
54
54
  }
55
55
 
56
- const topic = event;
57
- const channel = this.channel(topic);
56
+ const channel = this.channel(topic, params);
58
57
  channel
59
58
  .join()
60
59
  .receive('ok', (message) => {
61
- this._logger.debug('join success', { event, message });
60
+ this._logger.debug('join success', { topic, message });
62
61
  })
63
62
  .receive('error', (error) => {
64
- this._logger.error('join error', { event, error });
63
+ this._logger.error('join error', { topic, error });
65
64
  })
66
65
  .receive('timeout', () => {
67
- this._logger.debug('join timeout', { event });
66
+ this._logger.debug('join timeout', { topic });
68
67
  });
69
- channel.on(event, ({ status, response: data }) => {
68
+ channel.on(topic, ({ status, response: data }) => {
70
69
  if (status === 'ok') {
71
- this.emitter.emit(event, data);
70
+ this.emitter.emit(topic, data);
72
71
  } else {
73
- this._logger.debug('response error', { event, status, data });
72
+ this._logger.debug('response error', { topic, status, data });
74
73
  }
75
74
  });
76
75
  }
@@ -78,13 +77,12 @@ module.exports = (Socket, EventEmitter, transport) => {
78
77
  /**
79
78
  * private
80
79
  */
81
- ensureLeaveChannel(event) {
82
- const count = this.emitter.listenerCount(event);
80
+ ensureLeaveChannel(topic) {
81
+ const count = this.emitter.listenerCount(topic);
83
82
  if (count > 0) {
84
83
  return;
85
84
  }
86
85
 
87
- const topic = event;
88
86
  const channel = this.channels.find((c) => c.topic === topic);
89
87
  if (!channel) {
90
88
  return;
@@ -94,15 +92,43 @@ module.exports = (Socket, EventEmitter, transport) => {
94
92
  channel
95
93
  .leave()
96
94
  .receive('ok', (message) => {
97
- this._logger.debug('leave success', { event, message });
95
+ this._logger.debug('leave success', { topic, message });
98
96
  })
99
97
  .receive('error', (err) => {
100
- this._logger.error('leave error', { event, err });
98
+ this._logger.error('leave error', { topic, err });
101
99
  })
102
100
  .receive('timeout', () => {
103
- this._logger.debug('leave timeout', { event });
101
+ this._logger.debug('leave timeout', { topic });
104
102
  });
105
- channel.off(event);
103
+ channel.off(topic);
104
+ }
105
+
106
+ // More flexible subscribe
107
+ // Allow user to join a channel and listen to any POI events
108
+ subscribe(topic, params = {}) {
109
+ let channel = this.channels.find((c) => c.topic === topic);
110
+ if (channel) {
111
+ return channel;
112
+ }
113
+
114
+ channel = this.channel(topic, params);
115
+ channel
116
+ .join()
117
+ .receive('ok', (message) => {
118
+ this._logger.debug('join success', { topic, message });
119
+ })
120
+ .receive('error', (error) => {
121
+ this._logger.error('join error', { topic, error });
122
+ })
123
+ .receive('timeout', () => {
124
+ this._logger.debug('join timeout', { topic });
125
+ });
126
+
127
+ return channel;
128
+ }
129
+
130
+ unsubscribe(topic) {
131
+ this.ensureLeaveChannel(topic);
106
132
  }
107
133
  };
108
134
  };
@@ -106,7 +106,7 @@ class WsServer extends EventEmitter {
106
106
 
107
107
  /**
108
108
  * Broadcast message to all subscribers of a topic, can be used as
109
- * - broadcast(event, data)
109
+ * - broadcast(event, data) ==> broadcast(event, event, data)
110
110
  * - broadcast(topic, event, data)
111
111
  * - broadcast(topic, event, data, options)
112
112
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcblock/ws",
3
- "version": "1.18.45",
3
+ "version": "1.18.47",
4
4
  "description": "OCAP Chain websocket server and client",
5
5
  "keywords": [
6
6
  "websocket"
@@ -34,7 +34,7 @@
34
34
  "url": "https://github.com/ArcBlock/asset-chain/issues"
35
35
  },
36
36
  "dependencies": {
37
- "@arcblock/event-hub": "1.18.45",
37
+ "@arcblock/event-hub": "1.18.47",
38
38
  "debug": "^4.3.4",
39
39
  "eventemitter3": "^4.0.7",
40
40
  "lodash": "^4.17.21",
@@ -45,5 +45,5 @@
45
45
  "devDependencies": {
46
46
  "get-port": "^5.1.1"
47
47
  },
48
- "gitHead": "17a59291355d044599f211bd226af2756de91df0"
48
+ "gitHead": "84da7fb437e89f5c6a8e339aa01ab2e0378b8067"
49
49
  }