From f4a37b47dbdbadcd820e459553699892fd28ae1c Mon Sep 17 00:00:00 2001 From: "xiehao.0527" Date: Thu, 3 Jul 2025 18:59:42 +0800 Subject: [PATCH] fix: ws ping --- .gitignore | 1 + .../core/providers/asr/volcengine.py | 22 +++++++++---------- .../core/providers/tts/volcengine.py | 22 +++++++++---------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 9469baacc..9b2bffb3e 100644 --- a/.gitignore +++ b/.gitignore @@ -174,3 +174,4 @@ main/xiaozhi-server/mysql uploadfile *.json .vscode +deploy/ diff --git a/main/xiaozhi-server/core/providers/asr/volcengine.py b/main/xiaozhi-server/core/providers/asr/volcengine.py index 653c706ea..edcc1a85b 100644 --- a/main/xiaozhi-server/core/providers/asr/volcengine.py +++ b/main/xiaozhi-server/core/providers/asr/volcengine.py @@ -334,7 +334,7 @@ class ASRProvider(ASRProviderBase): if self.ws: logger.bind(tag=TAG).info(f"Checking WebSocket connection health: {self.ws_url}") # 发送ping并设置200ms超时 - #await asyncio.wait_for(self.ws.ping(), timeout=0.2) + await asyncio.wait_for(self.ws.ping(), timeout=0.2) logger.bind(tag=TAG).debug("WebSocket ping successful") return True logger.bind(tag=TAG).warning("WebSocket connection not open") @@ -345,16 +345,16 @@ class ASRProvider(ASRProviderBase): except Exception as e: logger.bind(tag=TAG).error(f"Failed to ping WebSocket: {e}") await self.stop_ws_connection() - - try: - logger.bind(tag=TAG).info(f"Connecting to {self.ws_url}") - headers = {"Authorization": f"Bearer {self.api_key}"} - self.ws = await websockets.connect(self.ws_url, additional_headers=headers) - logger.bind(tag=TAG).info("WebSocket connection established.") - except Exception as e: - logger.bind(tag=TAG).error(f"Failed to connect to WebSocket: {e}") - self.ws = None - raise + if self.ws is None: + try: + logger.bind(tag=TAG).info(f"Connecting to {self.ws_url}") + headers = {"Authorization": f"Bearer {self.api_key}"} + self.ws = await websockets.connect(self.ws_url, additional_headers=headers) + logger.bind(tag=TAG).info("WebSocket connection established.") + except Exception as e: + logger.bind(tag=TAG).error(f"Failed to connect to WebSocket: {e}") + self.ws = None + raise async def stop_ws_connection(self): """ diff --git a/main/xiaozhi-server/core/providers/tts/volcengine.py b/main/xiaozhi-server/core/providers/tts/volcengine.py index 052f187de..a0c1cb3d5 100644 --- a/main/xiaozhi-server/core/providers/tts/volcengine.py +++ b/main/xiaozhi-server/core/providers/tts/volcengine.py @@ -304,7 +304,7 @@ class TTSProvider(TTSProviderBase): if self.ws: logger.bind(tag=TAG).info(f"Checking WebSocket connection health: {self.ws_url}") # 发送ping并设置200ms超时 - #await asyncio.wait_for(self.ws.ping(), timeout=0.2) + await asyncio.wait_for(self.ws.ping(), timeout=0.2) logger.bind(tag=TAG).debug("WebSocket ping successful") return True logger.bind(tag=TAG).warning("WebSocket connection not open") @@ -315,16 +315,16 @@ class TTSProvider(TTSProviderBase): except Exception as e: logger.bind(tag=TAG).error(f"Failed to ping WebSocket: {e}") await self.stop_ws_connection() - - try: - logger.bind(tag=TAG).info(f"Connecting to {self.ws_url}") - headers = {"Authorization": f"Bearer {self.api_key}"} - self.ws = await websockets.connect(self.ws_url, additional_headers=headers) - logger.bind(tag=TAG).info("WebSocket connection established.") - except Exception as e: - logger.bind(tag=TAG).error(f"Failed to connect to WebSocket: {e}") - self.ws = None - raise + if self.ws is None: + try: + logger.bind(tag=TAG).info(f"Connecting to {self.ws_url}") + headers = {"Authorization": f"Bearer {self.api_key}"} + self.ws = await websockets.connect(self.ws_url, additional_headers=headers) + logger.bind(tag=TAG).info("WebSocket connection established.") + except Exception as e: + logger.bind(tag=TAG).error(f"Failed to connect to WebSocket: {e}") + self.ws = None + raise