From fbf58fd24c7554c7df5e3fddc6511d1dbf1d47c4 Mon Sep 17 00:00:00 2001 From: Yan Date: Wed, 4 Jun 2025 13:38:43 +0800 Subject: [PATCH] fix websocket reconnection --- strategies/grid_trader.py | 8 +------- ws_client/client.py | 4 +++- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/strategies/grid_trader.py b/strategies/grid_trader.py index 36ec269..a36e631 100644 --- a/strategies/grid_trader.py +++ b/strategies/grid_trader.py @@ -361,13 +361,7 @@ def check_ws_connection(self): # 嘗試關閉現有連接 if self.ws: try: - if hasattr(self.ws, 'running') and self.ws.running: - self.ws.running = False - if hasattr(self.ws, 'ws') and self.ws.ws: - try: - self.ws.ws.close() - except: - pass + # 使用 BackpackWebSocket 的 close 方法以確保完全斷開 self.ws.close() time.sleep(0.5) except Exception as e: diff --git a/ws_client/client.py b/ws_client/client.py index 1fd2496..01cd98c 100644 --- a/ws_client/client.py +++ b/ws_client/client.py @@ -250,10 +250,12 @@ def reconnect(self): on_pong=self.on_pong ) - # 創建新線程 + # 創建新線程並重新啟動心跳 + self.running = True self.ws_thread = threading.Thread(target=self.ws_run_forever) self.ws_thread.daemon = True self.ws_thread.start() + self.start_heartbeat() # 更新最後心跳時間,避免重連後立即觸發心跳檢測 self.last_heartbeat = time.time()