From f490c152825feae2319259b41a0b4c8925efb1f6 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Mon, 28 Apr 2025 12:01:50 +0800 Subject: [PATCH] =?UTF-8?q?update:=E4=BF=AE=E5=A4=8Dwebsocket=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E8=A2=AB=E6=B5=8F=E8=A7=88=E5=99=A8=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E5=87=BA=E9=94=99=E7=9A=84bug=20(#1034)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * update:修复websocket接口被浏览器访问出错的bug * update:修改版本号 --- .../java/xiaozhi/common/constant/Constant.java | 2 +- main/xiaozhi-server/config/logger.py | 2 +- main/xiaozhi-server/core/websocket_server.py | 15 +++++++++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java b/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java index cee9727e..07d746b8 100644 --- a/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java +++ b/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java @@ -177,5 +177,5 @@ public interface Constant { /** * 版本号 */ - public static final String VERSION = "0.3.12"; + public static final String VERSION = "0.3.13"; } \ No newline at end of file diff --git a/main/xiaozhi-server/config/logger.py b/main/xiaozhi-server/config/logger.py index f42ca6c0..9eed8540 100644 --- a/main/xiaozhi-server/config/logger.py +++ b/main/xiaozhi-server/config/logger.py @@ -3,7 +3,7 @@ import sys from loguru import logger from config.config_loader import load_config -SERVER_VERSION = "0.3.12" +SERVER_VERSION = "0.3.13" def get_module_abbreviation(module_name, module_dict): diff --git a/main/xiaozhi-server/core/websocket_server.py b/main/xiaozhi-server/core/websocket_server.py index 038379d2..fae94046 100644 --- a/main/xiaozhi-server/core/websocket_server.py +++ b/main/xiaozhi-server/core/websocket_server.py @@ -2,7 +2,7 @@ import asyncio import websockets from config.logger import setup_logging from core.connection import ConnectionHandler -from core.utils.util import get_local_ip, initialize_modules +from core.utils.util import initialize_modules TAG = __name__ @@ -27,7 +27,9 @@ class WebSocketServer: host = server_config.get("ip", "0.0.0.0") port = int(server_config.get("port", 8000)) - async with websockets.serve(self._handle_connection, host, port): + async with websockets.serve( + self._handle_connection, host, port, process_request=self._http_response + ): await asyncio.Future() async def _handle_connection(self, websocket): @@ -47,3 +49,12 @@ class WebSocketServer: await handler.handle_connection(websocket) finally: self.active_connections.discard(handler) + + async def _http_response(self, websocket, request_headers): + # 检查是否为 WebSocket 升级请求 + if request_headers.headers.get("connection", "").lower() == "upgrade": + # 如果是 WebSocket 请求,返回 None 允许握手继续 + return None + else: + # 如果是普通 HTTP 请求,返回 "server is running" + return websocket.respond(200, "Server is running\n")