@clonegod/ttd-core 2.0.75 → 2.0.77
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.
- package/dist/ws/ws_client.d.ts +1 -5
- package/dist/ws/ws_client.js +50 -46
- package/package.json +1 -1
package/dist/ws/ws_client.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ export interface WebSocketClientOptions {
|
|
|
3
3
|
[key: string]: string;
|
|
4
4
|
};
|
|
5
5
|
reconnectInterval?: number;
|
|
6
|
-
maxReconnectAttempts?: number;
|
|
7
6
|
rejectUnauthorized?: boolean;
|
|
8
7
|
keepAlive?: boolean;
|
|
9
8
|
keepAliveInterval?: number;
|
|
@@ -14,17 +13,14 @@ export declare class WebSocketClient {
|
|
|
14
13
|
private ws;
|
|
15
14
|
private onMessageCallback;
|
|
16
15
|
private onOpenCallback;
|
|
17
|
-
private reconnectAttempts;
|
|
18
|
-
private maxReconnectAttempts;
|
|
19
16
|
private keepAliveTimer;
|
|
20
17
|
constructor(url: string, options?: WebSocketClientOptions);
|
|
21
18
|
connect(): void;
|
|
19
|
+
private reconnect;
|
|
22
20
|
onMessage(callback: (data: any) => void): void;
|
|
23
21
|
onOpen(callback: () => void): void;
|
|
24
22
|
send(data: string): boolean;
|
|
25
23
|
disconnect(): void;
|
|
26
|
-
private handleReconnect;
|
|
27
|
-
private cleanup;
|
|
28
24
|
isConnected(): boolean;
|
|
29
25
|
private startKeepAlive;
|
|
30
26
|
private stopKeepAlive;
|
package/dist/ws/ws_client.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
13
|
};
|
|
@@ -11,41 +20,51 @@ class WebSocketClient {
|
|
|
11
20
|
this.ws = null;
|
|
12
21
|
this.onMessageCallback = null;
|
|
13
22
|
this.onOpenCallback = null;
|
|
14
|
-
this.reconnectAttempts = 0;
|
|
15
|
-
this.maxReconnectAttempts = 10;
|
|
16
23
|
this.keepAliveTimer = null;
|
|
17
24
|
this.url = url;
|
|
18
|
-
this.options = Object.assign({ reconnectInterval: 3000,
|
|
19
|
-
this.maxReconnectAttempts = this.options.maxReconnectAttempts;
|
|
25
|
+
this.options = Object.assign({ reconnectInterval: 3000, rejectUnauthorized: false, keepAlive: false, keepAliveInterval: 30000 }, options);
|
|
20
26
|
(0, dist_1.log_info)(`WebSocketClient constructor, url: ${this.url}`, this.options);
|
|
21
27
|
}
|
|
22
28
|
connect() {
|
|
23
29
|
(0, dist_1.log_info)(`Connecting to ${this.url}`);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
30
|
+
try {
|
|
31
|
+
this.ws = new ws_1.default(this.url, { headers: this.options.headers });
|
|
32
|
+
this.ws.on('open', () => {
|
|
33
|
+
(0, dist_1.log_info)('ws connected');
|
|
34
|
+
this.startKeepAlive();
|
|
35
|
+
if (this.onOpenCallback)
|
|
36
|
+
this.onOpenCallback();
|
|
37
|
+
});
|
|
38
|
+
this.ws.on('message', (message) => {
|
|
39
|
+
try {
|
|
40
|
+
const data = JSON.parse(message.toString());
|
|
41
|
+
if (this.onMessageCallback)
|
|
42
|
+
this.onMessageCallback(data);
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
(0, dist_1.log_error)('Invalid message format:', err);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
this.ws.on('close', (code, reason) => {
|
|
49
|
+
(0, dist_1.log_warn)(`ws disconnected: ${code} - ${reason.toString()}`);
|
|
50
|
+
this.reconnect();
|
|
51
|
+
});
|
|
52
|
+
this.ws.on('error', (err) => {
|
|
53
|
+
(0, dist_1.log_error)('ws error:', err);
|
|
54
|
+
this.reconnect();
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
(0, dist_1.log_error)('Failed to create WebSocket:', error);
|
|
59
|
+
this.reconnect();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
reconnect() {
|
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
(0, dist_1.log_warn)(`Reconnecting in ${this.options.reconnectInterval}ms`);
|
|
65
|
+
yield (0, dist_1.sleep)(this.options.reconnectInterval);
|
|
66
|
+
this.disconnect();
|
|
67
|
+
this.connect();
|
|
49
68
|
});
|
|
50
69
|
}
|
|
51
70
|
onMessage(callback) {
|
|
@@ -65,31 +84,16 @@ class WebSocketClient {
|
|
|
65
84
|
}
|
|
66
85
|
}
|
|
67
86
|
disconnect() {
|
|
68
|
-
this.cleanup();
|
|
69
|
-
}
|
|
70
|
-
handleReconnect() {
|
|
71
|
-
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
72
|
-
(0, dist_1.log_warn)(`Max reconnection attempts (${this.maxReconnectAttempts}) reached!`);
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
this.reconnectAttempts++;
|
|
76
|
-
(0, dist_1.log_warn)(`Reconnecting in ${this.options.reconnectInterval}ms (attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts})`);
|
|
77
|
-
setTimeout(() => {
|
|
78
|
-
this.cleanup();
|
|
79
|
-
this.connect();
|
|
80
|
-
}, this.options.reconnectInterval);
|
|
81
|
-
}
|
|
82
|
-
cleanup() {
|
|
83
87
|
this.stopKeepAlive();
|
|
84
88
|
if (this.ws) {
|
|
85
89
|
this.ws.removeAllListeners();
|
|
86
90
|
try {
|
|
87
|
-
if (this.ws.readyState === ws_1.default.OPEN
|
|
91
|
+
if (this.ws.readyState === ws_1.default.OPEN) {
|
|
88
92
|
this.ws.close();
|
|
89
93
|
}
|
|
90
94
|
}
|
|
91
95
|
catch (error) {
|
|
92
|
-
(0, dist_1.log_warn)(`Error closing WebSocket
|
|
96
|
+
(0, dist_1.log_warn)(`Error closing WebSocket: ${error.message}`);
|
|
93
97
|
}
|
|
94
98
|
this.ws = null;
|
|
95
99
|
}
|