fix: ws ping

This commit is contained in:
xiehao.0527 2025-07-03 18:59:42 +08:00
parent 491510936c
commit f4a37b47db
3 changed files with 23 additions and 22 deletions

1
.gitignore vendored
View file

@ -174,3 +174,4 @@ main/xiaozhi-server/mysql
uploadfile uploadfile
*.json *.json
.vscode .vscode
deploy/

View file

@ -334,7 +334,7 @@ class ASRProvider(ASRProviderBase):
if self.ws: if self.ws:
logger.bind(tag=TAG).info(f"Checking WebSocket connection health: {self.ws_url}") logger.bind(tag=TAG).info(f"Checking WebSocket connection health: {self.ws_url}")
# 发送ping并设置200ms超时 # 发送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") logger.bind(tag=TAG).debug("WebSocket ping successful")
return True return True
logger.bind(tag=TAG).warning("WebSocket connection not open") logger.bind(tag=TAG).warning("WebSocket connection not open")
@ -345,16 +345,16 @@ class ASRProvider(ASRProviderBase):
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"Failed to ping WebSocket: {e}") logger.bind(tag=TAG).error(f"Failed to ping WebSocket: {e}")
await self.stop_ws_connection() await self.stop_ws_connection()
if self.ws is None:
try: try:
logger.bind(tag=TAG).info(f"Connecting to {self.ws_url}") logger.bind(tag=TAG).info(f"Connecting to {self.ws_url}")
headers = {"Authorization": f"Bearer {self.api_key}"} headers = {"Authorization": f"Bearer {self.api_key}"}
self.ws = await websockets.connect(self.ws_url, additional_headers=headers) self.ws = await websockets.connect(self.ws_url, additional_headers=headers)
logger.bind(tag=TAG).info("WebSocket connection established.") logger.bind(tag=TAG).info("WebSocket connection established.")
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"Failed to connect to WebSocket: {e}") logger.bind(tag=TAG).error(f"Failed to connect to WebSocket: {e}")
self.ws = None self.ws = None
raise raise
async def stop_ws_connection(self): async def stop_ws_connection(self):
""" """

View file

@ -304,7 +304,7 @@ class TTSProvider(TTSProviderBase):
if self.ws: if self.ws:
logger.bind(tag=TAG).info(f"Checking WebSocket connection health: {self.ws_url}") logger.bind(tag=TAG).info(f"Checking WebSocket connection health: {self.ws_url}")
# 发送ping并设置200ms超时 # 发送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") logger.bind(tag=TAG).debug("WebSocket ping successful")
return True return True
logger.bind(tag=TAG).warning("WebSocket connection not open") logger.bind(tag=TAG).warning("WebSocket connection not open")
@ -315,16 +315,16 @@ class TTSProvider(TTSProviderBase):
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"Failed to ping WebSocket: {e}") logger.bind(tag=TAG).error(f"Failed to ping WebSocket: {e}")
await self.stop_ws_connection() await self.stop_ws_connection()
if self.ws is None:
try: try:
logger.bind(tag=TAG).info(f"Connecting to {self.ws_url}") logger.bind(tag=TAG).info(f"Connecting to {self.ws_url}")
headers = {"Authorization": f"Bearer {self.api_key}"} headers = {"Authorization": f"Bearer {self.api_key}"}
self.ws = await websockets.connect(self.ws_url, additional_headers=headers) self.ws = await websockets.connect(self.ws_url, additional_headers=headers)
logger.bind(tag=TAG).info("WebSocket connection established.") logger.bind(tag=TAG).info("WebSocket connection established.")
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"Failed to connect to WebSocket: {e}") logger.bind(tag=TAG).error(f"Failed to connect to WebSocket: {e}")
self.ws = None self.ws = None
raise raise