@clonegod/ttd-core 2.0.70 → 2.0.71
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 +4 -17
- package/dist/ws/ws_client.js +21 -83
- package/package.json +1 -1
package/dist/ws/ws_client.d.ts
CHANGED
|
@@ -1,43 +1,30 @@
|
|
|
1
1
|
export interface WebSocketClientOptions {
|
|
2
|
-
reconnectInterval?: number;
|
|
3
|
-
maxReconnectAttempts?: number;
|
|
4
2
|
headers?: {
|
|
5
3
|
[key: string]: string;
|
|
6
4
|
};
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
timeout?: number;
|
|
5
|
+
reconnectInterval?: number;
|
|
6
|
+
maxReconnectAttempts?: number;
|
|
7
|
+
rejectUnauthorized?: boolean;
|
|
11
8
|
keepAlive?: boolean;
|
|
12
9
|
keepAliveInterval?: number;
|
|
13
|
-
rejectUnauthorized?: boolean;
|
|
14
10
|
}
|
|
15
11
|
export declare class WebSocketClient {
|
|
16
12
|
private url;
|
|
17
13
|
private options;
|
|
18
14
|
private ws;
|
|
19
|
-
private reconnectInterval;
|
|
20
15
|
private onMessageCallback;
|
|
21
16
|
private onOpenCallback;
|
|
22
|
-
private onCloseCallback;
|
|
23
|
-
private onErrorCallback;
|
|
24
17
|
private reconnectAttempts;
|
|
25
18
|
private maxReconnectAttempts;
|
|
26
|
-
private isReconnecting;
|
|
27
19
|
private keepAliveTimer;
|
|
28
20
|
constructor(url: string, options?: WebSocketClientOptions);
|
|
29
|
-
private setDefaultOptions;
|
|
30
21
|
connect(): void;
|
|
31
22
|
onMessage(callback: (data: any) => void): void;
|
|
32
23
|
onOpen(callback: () => void): void;
|
|
33
|
-
|
|
34
|
-
onError(callback: (error: Error) => void): void;
|
|
35
|
-
send(data: any): boolean;
|
|
24
|
+
send(data_obj: string): boolean;
|
|
36
25
|
disconnect(): void;
|
|
37
26
|
private handleReconnect;
|
|
38
27
|
isConnected(): boolean;
|
|
39
|
-
getConnectionState(): string;
|
|
40
|
-
resetReconnectAttempts(): void;
|
|
41
28
|
private startKeepAlive;
|
|
42
29
|
private stopKeepAlive;
|
|
43
30
|
}
|
package/dist/ws/ws_client.js
CHANGED
|
@@ -4,55 +4,27 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.WebSocketClient = void 0;
|
|
7
|
+
const dist_1 = require("@clonegod/ttd-core/dist");
|
|
7
8
|
const ws_1 = __importDefault(require("ws"));
|
|
8
|
-
const __1 = require("..");
|
|
9
9
|
class WebSocketClient {
|
|
10
10
|
constructor(url, options = {}) {
|
|
11
11
|
this.ws = null;
|
|
12
12
|
this.onMessageCallback = null;
|
|
13
13
|
this.onOpenCallback = null;
|
|
14
|
-
this.onCloseCallback = null;
|
|
15
|
-
this.onErrorCallback = null;
|
|
16
14
|
this.reconnectAttempts = 0;
|
|
17
15
|
this.maxReconnectAttempts = 10;
|
|
18
|
-
this.isReconnecting = false;
|
|
19
16
|
this.keepAliveTimer = null;
|
|
20
17
|
this.url = url;
|
|
21
|
-
this.options =
|
|
22
|
-
this.reconnectInterval = this.options.reconnectInterval;
|
|
18
|
+
this.options = Object.assign({ reconnectInterval: 3000, maxReconnectAttempts: 10, rejectUnauthorized: false, keepAlive: false, keepAliveInterval: 30000 }, options);
|
|
23
19
|
this.maxReconnectAttempts = this.options.maxReconnectAttempts;
|
|
24
|
-
(0,
|
|
25
|
-
}
|
|
26
|
-
setDefaultOptions(options) {
|
|
27
|
-
const defaults = {
|
|
28
|
-
reconnectInterval: 3000,
|
|
29
|
-
maxReconnectAttempts: 10,
|
|
30
|
-
keepAlive: true,
|
|
31
|
-
keepAliveInterval: 30000,
|
|
32
|
-
handshakeTimeout: 10000,
|
|
33
|
-
perMessageDeflate: true,
|
|
34
|
-
timeout: 5000,
|
|
35
|
-
rejectUnauthorized: false,
|
|
36
|
-
};
|
|
37
|
-
return Object.assign(Object.assign({}, defaults), options);
|
|
20
|
+
(0, dist_1.log_info)(`WebSocketClient constructor, url: ${this.url}`, this.options);
|
|
38
21
|
}
|
|
39
22
|
connect() {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
const wsOptions = {
|
|
44
|
-
headers: this.options.headers,
|
|
45
|
-
protocols: this.options.protocols,
|
|
46
|
-
handshakeTimeout: this.options.handshakeTimeout,
|
|
47
|
-
perMessageDeflate: this.options.perMessageDeflate,
|
|
48
|
-
rejectUnauthorized: this.options.rejectUnauthorized
|
|
49
|
-
};
|
|
50
|
-
(0, __1.log_info)(`Connecting to ${this.url} with options:`, wsOptions);
|
|
51
|
-
this.ws = new ws_1.default(this.url, wsOptions);
|
|
23
|
+
(0, dist_1.log_info)(`Connecting to ${this.url}`);
|
|
24
|
+
this.ws = new ws_1.default(this.url, { headers: this.options.headers });
|
|
52
25
|
this.ws.on('open', () => {
|
|
53
|
-
|
|
26
|
+
(0, dist_1.log_info)('ws connected');
|
|
54
27
|
this.reconnectAttempts = 0;
|
|
55
|
-
this.isReconnecting = false;
|
|
56
28
|
this.startKeepAlive();
|
|
57
29
|
if (this.onOpenCallback)
|
|
58
30
|
this.onOpenCallback();
|
|
@@ -64,19 +36,16 @@ class WebSocketClient {
|
|
|
64
36
|
this.onMessageCallback(data);
|
|
65
37
|
}
|
|
66
38
|
catch (err) {
|
|
67
|
-
|
|
39
|
+
(0, dist_1.log_error)('Invalid message format:', err);
|
|
68
40
|
}
|
|
69
41
|
});
|
|
70
42
|
this.ws.on('close', (code, reason) => {
|
|
71
|
-
|
|
72
|
-
if (this.onCloseCallback)
|
|
73
|
-
this.onCloseCallback();
|
|
43
|
+
(0, dist_1.log_warn)(`ws disconnected: ${code} - ${reason.toString()}`);
|
|
74
44
|
this.handleReconnect();
|
|
75
45
|
});
|
|
76
46
|
this.ws.on('error', (err) => {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
this.onErrorCallback(err);
|
|
47
|
+
(0, dist_1.log_error)('ws error:', err);
|
|
48
|
+
this.handleReconnect();
|
|
80
49
|
});
|
|
81
50
|
}
|
|
82
51
|
onMessage(callback) {
|
|
@@ -85,24 +54,17 @@ class WebSocketClient {
|
|
|
85
54
|
onOpen(callback) {
|
|
86
55
|
this.onOpenCallback = callback;
|
|
87
56
|
}
|
|
88
|
-
|
|
89
|
-
this.onCloseCallback = callback;
|
|
90
|
-
}
|
|
91
|
-
onError(callback) {
|
|
92
|
-
this.onErrorCallback = callback;
|
|
93
|
-
}
|
|
94
|
-
send(data) {
|
|
57
|
+
send(data_obj) {
|
|
95
58
|
if (this.ws && this.ws.readyState === ws_1.default.OPEN) {
|
|
96
|
-
this.ws.send(JSON.stringify(
|
|
59
|
+
this.ws.send(JSON.stringify(data_obj));
|
|
97
60
|
return true;
|
|
98
61
|
}
|
|
99
62
|
else {
|
|
100
|
-
|
|
63
|
+
(0, dist_1.log_warn)('ws is not connected, you need connect first, then send message');
|
|
101
64
|
return false;
|
|
102
65
|
}
|
|
103
66
|
}
|
|
104
67
|
disconnect() {
|
|
105
|
-
this.isReconnecting = false;
|
|
106
68
|
this.stopKeepAlive();
|
|
107
69
|
if (this.ws) {
|
|
108
70
|
this.ws.close();
|
|
@@ -110,55 +72,30 @@ class WebSocketClient {
|
|
|
110
72
|
}
|
|
111
73
|
}
|
|
112
74
|
handleReconnect() {
|
|
113
|
-
if (this.isReconnecting) {
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
75
|
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
117
|
-
(0,
|
|
76
|
+
(0, dist_1.log_warn)(`Max reconnection attempts (${this.maxReconnectAttempts}) reached`);
|
|
118
77
|
return;
|
|
119
78
|
}
|
|
120
|
-
this.isReconnecting = true;
|
|
121
79
|
this.reconnectAttempts++;
|
|
122
|
-
(0,
|
|
80
|
+
(0, dist_1.log_warn)(`Reconnecting in ${this.options.reconnectInterval}ms (attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts})`);
|
|
123
81
|
setTimeout(() => {
|
|
124
|
-
(0, __1.log_info)(`Attempting to reconnect to ${this.url}... (${this.reconnectAttempts}/${this.maxReconnectAttempts})`);
|
|
125
82
|
this.connect();
|
|
126
|
-
}, this.reconnectInterval);
|
|
83
|
+
}, this.options.reconnectInterval);
|
|
127
84
|
}
|
|
128
85
|
isConnected() {
|
|
129
86
|
return this.ws !== null && this.ws.readyState === ws_1.default.OPEN;
|
|
130
87
|
}
|
|
131
|
-
getConnectionState() {
|
|
132
|
-
if (!this.ws)
|
|
133
|
-
return 'DISCONNECTED';
|
|
134
|
-
switch (this.ws.readyState) {
|
|
135
|
-
case ws_1.default.CONNECTING:
|
|
136
|
-
return 'CONNECTING';
|
|
137
|
-
case ws_1.default.OPEN:
|
|
138
|
-
return 'OPEN';
|
|
139
|
-
case ws_1.default.CLOSING:
|
|
140
|
-
return 'CLOSING';
|
|
141
|
-
case ws_1.default.CLOSED:
|
|
142
|
-
return 'CLOSED';
|
|
143
|
-
default:
|
|
144
|
-
return 'UNKNOWN';
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
resetReconnectAttempts() {
|
|
148
|
-
this.reconnectAttempts = 0;
|
|
149
|
-
this.isReconnecting = false;
|
|
150
|
-
}
|
|
151
88
|
startKeepAlive() {
|
|
152
89
|
if (!this.options.keepAlive) {
|
|
153
|
-
(0, __1.log_info)('KeepAlive disabled');
|
|
154
90
|
return;
|
|
155
91
|
}
|
|
156
|
-
|
|
157
|
-
|
|
92
|
+
this.stopKeepAlive();
|
|
93
|
+
const interval = this.options.keepAliveInterval || 30000;
|
|
94
|
+
(0, dist_1.log_info)(`Starting keepAlive with interval: ${interval}ms`);
|
|
158
95
|
this.keepAliveTimer = setInterval(() => {
|
|
159
96
|
if (this.ws && this.ws.readyState === ws_1.default.OPEN) {
|
|
160
97
|
this.ws.ping();
|
|
161
|
-
(0,
|
|
98
|
+
(0, dist_1.log_info)('KeepAlive ping sent');
|
|
162
99
|
}
|
|
163
100
|
}, interval);
|
|
164
101
|
}
|
|
@@ -166,6 +103,7 @@ class WebSocketClient {
|
|
|
166
103
|
if (this.keepAliveTimer) {
|
|
167
104
|
clearInterval(this.keepAliveTimer);
|
|
168
105
|
this.keepAliveTimer = null;
|
|
106
|
+
(0, dist_1.log_info)('KeepAlive stopped');
|
|
169
107
|
}
|
|
170
108
|
}
|
|
171
109
|
}
|