@softtynet/zca-js 2.1.8 → 2.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -42,6 +42,8 @@ interface ListenerEvents {
42
42
  friend_event: [data: FriendEvent];
43
43
  group_event: [data: GroupEvent];
44
44
  cipher_key: [key: string];
45
+ ping: [];
46
+ pong: [];
45
47
  }
46
48
  export declare class Listener extends EventEmitter<ListenerEvents> {
47
49
  private ctx;
@@ -59,6 +61,7 @@ export declare class Listener extends EventEmitter<ListenerEvents> {
59
61
  private cipherKey?;
60
62
  private selfListen;
61
63
  private pingInterval?;
64
+ private retryTimeout?;
62
65
  private id;
63
66
  constructor(ctx: ContextSession, urls: string[]);
64
67
  /**
@@ -129,7 +129,7 @@ export class Listener extends EventEmitter {
129
129
  if (shouldRotate) {
130
130
  this.rotateEndpoint();
131
131
  }
132
- setTimeout(() => {
132
+ this.retryTimeout = setTimeout(() => {
133
133
  this.start({ retryOnClose: true });
134
134
  }, retry);
135
135
  }
@@ -143,6 +143,7 @@ export class Listener extends EventEmitter {
143
143
  this.emit("error", event);
144
144
  };
145
145
  ws.onmessage = async (event) => {
146
+ this.emit("pong"); // Pulse event to notify watchdog that socket is alive
146
147
  const { data } = event;
147
148
  if (!(data instanceof Buffer))
148
149
  return;
@@ -167,6 +168,7 @@ export class Listener extends EventEmitter {
167
168
  data: { eventId: Date.now() },
168
169
  };
169
170
  this.sendWs(payload, false);
171
+ this.emit("ping");
170
172
  };
171
173
  this.pingInterval = setInterval(() => {
172
174
  ping();
@@ -355,16 +357,24 @@ export class Listener extends EventEmitter {
355
357
  }
356
358
  }
357
359
  catch (error) {
360
+ if (error instanceof ZaloApiError && error.message.includes("Invalid data length or missing cipher key")) {
361
+ logger(this.ctx).verbose("Ignored undecryptable Zalo packet. This is normal for some reaction/system events.");
362
+ return;
363
+ }
358
364
  this.onErrorCallback(error);
359
365
  this.emit("error", error);
360
366
  }
361
367
  };
362
368
  }
363
369
  stop() {
370
+ if (this.retryTimeout)
371
+ clearTimeout(this.retryTimeout);
364
372
  if (this.ws) {
365
- this.ws.close(CloseReason.ManualClosure);
366
- this.reset();
373
+ if (this.ws.readyState !== WebSocket.CLOSED) {
374
+ this.ws.close(CloseReason.ManualClosure);
375
+ }
367
376
  }
377
+ this.reset();
368
378
  }
369
379
  sendWs(payload, requireId = true) {
370
380
  if (this.ws) {
@@ -141,7 +141,7 @@ class Listener extends EventEmitter {
141
141
  if (shouldRotate) {
142
142
  this.rotateEndpoint();
143
143
  }
144
- setTimeout(() => {
144
+ this.retryTimeout = setTimeout(() => {
145
145
  this.start({ retryOnClose: true });
146
146
  }, retry);
147
147
  }
@@ -155,6 +155,7 @@ class Listener extends EventEmitter {
155
155
  this.emit("error", event);
156
156
  };
157
157
  ws.onmessage = async (event) => {
158
+ this.emit("pong"); // Pulse event to notify watchdog that socket is alive
158
159
  const { data } = event;
159
160
  if (!(data instanceof Buffer))
160
161
  return;
@@ -179,6 +180,7 @@ class Listener extends EventEmitter {
179
180
  data: { eventId: Date.now() },
180
181
  };
181
182
  this.sendWs(payload, false);
183
+ this.emit("ping");
182
184
  };
183
185
  this.pingInterval = setInterval(() => {
184
186
  ping();
@@ -367,16 +369,24 @@ class Listener extends EventEmitter {
367
369
  }
368
370
  }
369
371
  catch (error) {
372
+ if (error instanceof ZaloApiError.ZaloApiError && error.message.includes("Invalid data length or missing cipher key")) {
373
+ utils.logger(this.ctx).verbose("Ignored undecryptable Zalo packet. This is normal for some reaction/system events.");
374
+ return;
375
+ }
370
376
  this.onErrorCallback(error);
371
377
  this.emit("error", error);
372
378
  }
373
379
  };
374
380
  }
375
381
  stop() {
382
+ if (this.retryTimeout)
383
+ clearTimeout(this.retryTimeout);
376
384
  if (this.ws) {
377
- this.ws.close(exports.CloseReason.ManualClosure);
378
- this.reset();
385
+ if (this.ws.readyState !== WebSocket.CLOSED) {
386
+ this.ws.close(exports.CloseReason.ManualClosure);
387
+ }
379
388
  }
389
+ this.reset();
380
390
  }
381
391
  sendWs(payload, requireId = true) {
382
392
  if (this.ws) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softtynet/zca-js",
3
- "version": "2.1.8",
3
+ "version": "2.1.10",
4
4
  "description": "Unofficial Zalo API for JavaScript",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",