@clonegod/ttd-bsc-common 1.0.19 → 1.0.20

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.
@@ -23,7 +23,7 @@ export declare class PoolEventListener {
23
23
  init(poolList: StandardPoolInfoType[]): Promise<void>;
24
24
  start(): Promise<void>;
25
25
  stop(): Promise<void>;
26
- private connect;
26
+ connect(): Promise<void>;
27
27
  private handleConnectionIssue;
28
28
  private startBlockListener;
29
29
  private stopBlockListener;
@@ -61,37 +61,41 @@ class PoolEventListener {
61
61
  }
62
62
  connect() {
63
63
  return __awaiter(this, void 0, void 0, function* () {
64
- try {
65
- (0, dist_1.log_info)(`正在连接 WebSocket: ${this.ws_endpoint}`);
66
- this.wsProvider = new ethers_1.ethers.providers.WebSocketProvider(this.ws_endpoint);
67
- yield this.wsProvider.ready;
68
- this.isConnected = true;
69
- this.reconnectAttempts = 0;
70
- (0, dist_1.log_info)(`WebSocket 已连接: ${this.ws_endpoint}`);
71
- this.wsProvider.on('error', (err) => {
72
- (0, dist_1.log_error)(`WebSocket 错误:`, err, '');
73
- this.handleConnectionIssue();
74
- });
75
- this.wsProvider._websocket.on('close', (code, reason) => {
76
- (0, dist_1.log_warn)(`WebSocket 连接关闭, 代码: ${code}, 原因: ${reason || '未知'}`);
77
- this.handleConnectionIssue();
78
- });
79
- if (this.isStarted) {
80
- yield this.startBlockListener();
81
- }
82
- }
83
- catch (error) {
84
- (0, dist_1.log_error)(`首次连接 WebSocket 失败: ${this.ws_endpoint}`, error, '');
85
- if (this.reconnectAttempts > 0) {
86
- this.handleConnectionIssue();
64
+ const maxRetries = 3;
65
+ for (let i = 0; i < maxRetries; i++) {
66
+ try {
67
+ (0, dist_1.log_info)(`正在连接 WebSocket: ${this.ws_endpoint} (尝试 ${i + 1}/${maxRetries})`);
68
+ this.wsProvider = new ethers_1.ethers.providers.WebSocketProvider(this.ws_endpoint);
69
+ const timeout = 10000;
70
+ const wsPromise = this.wsProvider.ready;
71
+ const timeoutPromise = new Promise((_, reject) => {
72
+ setTimeout(() => reject(new Error('WebSocket connection timeout')), timeout);
73
+ });
74
+ yield Promise.race([wsPromise, timeoutPromise]);
75
+ this.isConnected = true;
76
+ this.reconnectAttempts = 0;
77
+ (0, dist_1.log_info)(`WebSocket 已连接: ${this.ws_endpoint}`);
78
+ this.wsProvider.on('error', (err) => {
79
+ (0, dist_1.log_error)(`WebSocket 错误:`, err, '');
80
+ this.handleConnectionIssue();
81
+ });
82
+ this.wsProvider._websocket.on('close', (code, reason) => {
83
+ (0, dist_1.log_warn)(`WebSocket 连接关闭, 代码: ${code}, 原因: ${reason || '未知'}`);
84
+ this.handleConnectionIssue();
85
+ });
86
+ if (this.isStarted) {
87
+ yield this.startBlockListener();
88
+ }
89
+ return;
87
90
  }
88
- else {
89
- this.isReconnecting = true;
90
- this.isConnected = false;
91
- this.cleanup();
92
- yield (0, dist_1.sleep)(this.baseReconnectDelay);
93
- yield this.connect();
94
- this.isReconnecting = false;
91
+ catch (error) {
92
+ (0, dist_1.log_error)(`WebSocket 连接失败 (尝试 ${i + 1}/${maxRetries}):`, error);
93
+ if (i === maxRetries - 1) {
94
+ throw error;
95
+ }
96
+ const delay = 1000 * (i + 1);
97
+ (0, dist_1.log_info)(`等待 ${delay / 1000} 秒后重试...`);
98
+ yield new Promise(resolve => setTimeout(resolve, delay));
95
99
  }
96
100
  }
97
101
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-bsc-common",
3
- "version": "1.0.19",
3
+ "version": "1.0.20",
4
4
  "description": "BSC common library",
5
5
  "license": "UNLICENSED",
6
6
  "main": "dist/index.js",