update:修复websocket接口被浏览器访问出错的bug (#1034)

* update:修复websocket接口被浏览器访问出错的bug

* update:修改版本号
This commit is contained in:
hrz
2025-04-28 12:01:50 +08:00
committed by GitHub
parent f9f6a69c89
commit f490c15282
3 changed files with 15 additions and 4 deletions
@@ -177,5 +177,5 @@ public interface Constant {
/**
* 版本号
*/
public static final String VERSION = "0.3.12";
public static final String VERSION = "0.3.13";
}
+1 -1
View File
@@ -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):
+13 -2
View File
@@ -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")