mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
fix: ws ping
This commit is contained in:
parent
491510936c
commit
f4a37b47db
3 changed files with 23 additions and 22 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -174,3 +174,4 @@ main/xiaozhi-server/mysql
|
||||||
uploadfile
|
uploadfile
|
||||||
*.json
|
*.json
|
||||||
.vscode
|
.vscode
|
||||||
|
deploy/
|
||||||
|
|
|
||||||
|
|
@ -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):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue