From 43edae2f90b7527961a211f2c515fc7b1239e309 Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Wed, 21 Jan 2026 09:56:48 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=B5=8B=E8=AF=95=E5=9C=A8=E5=AE=9D?= =?UTF-8?q?=E5=A1=94=E6=9C=8D=E5=8A=A1=E5=99=A8=E4=B8=8A=E7=9A=84=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=8F=8D=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/nginx/sites-enabled/app.conf | 6 ++++-- src/addon/aikefu/docs/ws_multi_addon_test.html | 2 +- src/ws_server.php | 17 ++++++++++++++--- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/docker/nginx/sites-enabled/app.conf b/docker/nginx/sites-enabled/app.conf index 81015fca9..9b188ecce 100644 --- a/docker/nginx/sites-enabled/app.conf +++ b/docker/nginx/sites-enabled/app.conf @@ -1,9 +1,11 @@ server { - listen 80; + # 作为默认站点接管所有 Host(域名/IP) + listen 80 default_server; # listen 443 ssl http2; # Enable HTTP/2 - server_name localhost; + # 匹配任意域名/IP(Host 不限制) + server_name _ localhost 127.0.0.1; root /var/www/html; index index.php index.html index.htm default.php default.htm default.html; diff --git a/src/addon/aikefu/docs/ws_multi_addon_test.html b/src/addon/aikefu/docs/ws_multi_addon_test.html index fe9adfe4d..4505582e5 100644 --- a/src/addon/aikefu/docs/ws_multi_addon_test.html +++ b/src/addon/aikefu/docs/ws_multi_addon_test.html @@ -197,7 +197,7 @@ // 设置WebSocket服务器地址 const setWebsocketUrl = () => { - if (websocketUrl.value.trim() && websocketUrl.value.startsWith('ws://')) { + if (websocketUrl.value.trim() && (websocketUrl.value.startsWith('ws://') || websocketUrl.value.startsWith('wss://'))) { addons.forEach(addon => { addon.fullPath = websocketUrl.value + addon.path; addon.status = 'disconnected'; diff --git a/src/ws_server.php b/src/ws_server.php index ee8d05bcb..f3df9c742 100644 --- a/src/ws_server.php +++ b/src/ws_server.php @@ -64,7 +64,16 @@ use Ratchet\ConnectionInterface; use Ratchet\MessageComponentInterface; // 配置WebSocket服务器 -$httpHost = 'localhost'; // 客户端连接时使用的主机名 +// Ratchet\App 的 $httpHost 会被用于路由 Host 匹配(默认要求必须等于 ws:// 的 host) +// 注意:Ratchet\App 构造函数内部会把 $httpHost 传给 FlashPolicy::addAllowedAccess(), +// 为空会触发 “Invalid domain”。因此这里必须是一个合法域名/IP(用 localhost 兜底)。 +// 若希望通过任意 IP/域名访问:不要在构造里用空值;而是在 route(...) 的第4个参数传 '', +// 让该路由不限制 Host。 +$httpHost = getenv('WS_HTTP_HOST'); +$httpHost = ($httpHost === false) ? 'localhost' : trim((string)$httpHost); +if ($httpHost === '') { + $httpHost = 'localhost'; +} $port = getenv('WS_PORT') ?: 8080; // WebSocket服务器端口 $address = '0.0.0.0'; // 监听所有网络接口 @@ -193,7 +202,8 @@ foreach ($current_addon_names as $addonName) { if (class_exists($webSocketClass)) { // 注册到/ws/{addonName}路径 $path = '/ws/' . $addonName; - $ratchetApp->route($path, new $webSocketClass(), array('*')); + // 允许任意 Origin,并且不限制 Host(支持通过任意 IP/域名访问) + $ratchetApp->route($path, new $webSocketClass(), array('*'), ''); echo "已注册WebSocket控制器:{$webSocketClass} 到路径 {$path}\n"; $registeredAddons[] = $addonName; } else { @@ -260,7 +270,8 @@ class DefaultWebSocketController implements MessageComponentInterface } // 注册默认的/ws路径测试控制器 -$ratchetApp->route('/ws', new DefaultWebSocketController(), array('*')); +// 默认测试路径同样不限制 Host +$ratchetApp->route('/ws', new DefaultWebSocketController(), array('*'), ''); echo "已注册默认WebSocket测试控制器到路径 /ws\n"; // 缓存WebSocket服务器信息(可选,用于其他服务查询)