From 186f69c87ac8b49fb186609f53b76a3b5e42accd Mon Sep 17 00:00:00 2001 From: hubian <908234780@qq.com> Date: Sun, 12 Apr 2026 18:10:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20WebSocket=E6=96=AD=E5=BC=80=E5=90=8E?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E9=80=80=E5=87=BA=E5=BE=AA=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main_v2.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/main_v2.py b/main_v2.py index 95b5003..ca2fcf0 100644 --- a/main_v2.py +++ b/main_v2.py @@ -530,7 +530,27 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str): try: while True: - data = await websocket.receive_json() + try: + data = await websocket.receive_json() + except Exception as json_err: + logger.error(f"JSON解析错误: {json_err}") + # 如果连接已断开,退出循环 + if "disconnect" in str(json_err).lower() or "closed" in str(json_err).lower(): + logger.info("WebSocket已断开,退出循环") + break + try: + text_data = await websocket.receive_text() + if text_data.strip(): + data = json.loads(text_data) + else: + continue + except Exception as text_err: + logger.error(f"文本消息解析错误: {text_err}") + if "disconnect" in str(text_err).lower() or "closed" in str(text_err).lower(): + logger.info("WebSocket已断开,退出循环") + break + continue + action = data.get("action") logger.info(f"WebSocket收到消息: action={action}")