mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-28 10:03:54 +08:00
update:修复websocket接口被浏览器访问出错的bug
This commit is contained in:
@@ -2,7 +2,7 @@ import asyncio
|
|||||||
import websockets
|
import websockets
|
||||||
from config.logger import setup_logging
|
from config.logger import setup_logging
|
||||||
from core.connection import ConnectionHandler
|
from core.connection import ConnectionHandler
|
||||||
from core.utils.util import get_local_ip, initialize_modules
|
from core.utils.util import initialize_modules
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
|
|
||||||
@@ -27,7 +27,9 @@ class WebSocketServer:
|
|||||||
host = server_config.get("ip", "0.0.0.0")
|
host = server_config.get("ip", "0.0.0.0")
|
||||||
port = int(server_config.get("port", 8000))
|
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()
|
await asyncio.Future()
|
||||||
|
|
||||||
async def _handle_connection(self, websocket):
|
async def _handle_connection(self, websocket):
|
||||||
@@ -47,3 +49,12 @@ class WebSocketServer:
|
|||||||
await handler.handle_connection(websocket)
|
await handler.handle_connection(websocket)
|
||||||
finally:
|
finally:
|
||||||
self.active_connections.discard(handler)
|
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