mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 23:23:55 +08:00
update:修复websocket接口被浏览器访问出错的bug (#1034)
* update:修复websocket接口被浏览器访问出错的bug * update:修改版本号
This commit is contained in:
@@ -177,5 +177,5 @@ public interface Constant {
|
||||
/**
|
||||
* 版本号
|
||||
*/
|
||||
public static final String VERSION = "0.3.12";
|
||||
public static final String VERSION = "0.3.13";
|
||||
}
|
||||
@@ -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):
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user