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