From c3fec15f118817452fc632ecf965d0d0c48815d5 Mon Sep 17 00:00:00 2001 From: xiamuceer Date: Fri, 16 May 2025 17:00:06 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E6=B7=BB=E5=8A=A0ws=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E9=87=8D=E8=BF=9E=E6=9C=BA=E5=88=B6=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E8=BF=87=E9=95=BF=E6=97=B6=E9=97=B4=E6=94=B6=E4=B8=8D?= =?UTF-8?q?=E5=88=B0=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sources/wechatpadpro/wechatpadpro_adapter.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py b/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py index 9719299c5..c86df604c 100644 --- a/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py +++ b/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py @@ -321,21 +321,26 @@ class WeChatPadProAdapter(Platform): async with websockets.connect(ws_url) as websocket: self._websocket = websocket logger.info("WebSocket 连接成功。") + #设置空闲超时重连 + wait_time = 120 while True: try: - message = await websocket.recv() + message = await asyncio.wait_for(websocket.recv(), timeout=wait_time) + logger.info(message) asyncio.create_task(self.handle_websocket_message(message)) + except asyncio.TimeoutError: + # 10 分钟内没有收到消息,断开连接并重新尝试 + logger.warning(f"WebSocket 连接空闲超过 {wait_time} 分钟,尝试重新连接。") + break # 跳出内层循环,外层循环会处理重连 except websockets.exceptions.ConnectionClosedOK: logger.info("WebSocket 连接正常关闭。") break except Exception as e: logger.error(f"处理 WebSocket 消息时发生错误: {e}") - # 在这里可以添加重连逻辑 break except Exception as e: logger.error(f"WebSocket 连接失败: {e}") - # 在这里可以添加重连逻辑 - await asyncio.sleep(5) # 等待一段时间后重试 + await asyncio.sleep(5) # 连接失败时,等待 5 秒后重试 async def handle_websocket_message(self, message: str): """