violetics 7.0.14-alpha → 7.0.15-alpha

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,8 +42,45 @@ export class WebSocketClient extends AbstractSocketClient {
42
42
  this.socketListeners.set(event, handler);
43
43
  this.socket?.on(event, handler);
44
44
  }
45
+
46
+ this.socket.on('close', (code, reason) => {
47
+ this.emit('close', code, reason);
48
+ this.attemptReconnect(code, reason);
49
+ });
50
+ this.socketListeners.set('close-internal', (...args) => this.socket?.emit('close', ...args));
51
+ }
52
+
53
+ async attemptReconnect(code, reason) {
54
+ if (this.reconnecting || code === 1000 || this.reconnectAttempts >= this.maxReconnectAttempts) {
55
+ return;
56
+ }
57
+
58
+ this.reconnecting = true;
59
+ const delay = Math.min(
60
+ this.reconnectDelay * Math.pow(2, this.reconnectAttempts),
61
+ this.maxReconnectDelay
62
+ );
63
+
64
+ this.emit('reconnecting', {
65
+ attempt: this.reconnectAttempts + 1,
66
+ maxAttempts: this.maxReconnectAttempts,
67
+ delay,
68
+ code,
69
+ reason: reason?.toString() || 'Connection closed'
70
+ });
71
+
72
+ await new Promise(resolve => setTimeout(resolve, delay));
73
+
74
+ this.reconnectAttempts++;
75
+ this.reconnecting = false;
76
+
77
+ this.connect();
45
78
  }
79
+
46
80
  async close() {
81
+ this.reconnecting = false;
82
+ this.reconnectAttempts = 0;
83
+
47
84
  if (!this.socket) {
48
85
  return;
49
86
  }
@@ -54,13 +91,33 @@ export class WebSocketClient extends AbstractSocketClient {
54
91
  const closePromise = new Promise(resolve => {
55
92
  this.socket?.once('close', resolve);
56
93
  });
57
- this.socket.close();
58
- await closePromise;
94
+ this.socket.close(1000, 'Intentional close');
95
+
96
+ try {
97
+ await Promise.race([
98
+ closePromise,
99
+ new Promise((_, reject) => setTimeout(() => reject(new Error('close timeout')), 5000))
100
+ ]);
101
+ }
102
+ catch { }
103
+
59
104
  this.socket = null;
60
105
  }
106
+
61
107
  send(str, cb) {
62
108
  this.socket?.send(str, cb);
63
109
  return Boolean(this.socket);
64
110
  }
111
+
112
+ get connectionStats() {
113
+ return {
114
+ isOpen: this.isOpen,
115
+ isClosed: this.isClosed,
116
+ isConnecting: this.isConnecting,
117
+ reconnectAttempts: this.reconnectAttempts,
118
+ maxReconnectAttempts: this.maxReconnectAttempts,
119
+ isReconnecting: this.reconnecting
120
+ };
121
+ }
65
122
  }
66
123
  //# sourceMappingURL=websocket.js.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "violetics",
3
3
  "type": "module",
4
- "version": "7.0.14-alpha",
4
+ "version": "7.0.15-alpha",
5
5
  "description": "A WebSockets library for interacting with WhatsApp Web",
6
6
  "keywords": [
7
7
  "whatsapp",