Files
JoyD/Claw/scripts/test_websocket.bat
2026-03-16 15:47:55 +08:00

104 lines
3.6 KiB
Batchfile

@echo off
echo 🧪 开始测试 WebSocket 连接...
echo 📅 测试时间: %date% %time%
REM 设置测试参数
set GATEWAY_URL=ws://localhost:8000
set TEST_TIMEOUT=5
echo 📋 测试配置:
echo 网关 WebSocket URL: %GATEWAY_URL%
echo 测试超时时间: %TEST_TIMEOUT%
REM 检查 curl 是否可用
where curl >nul 2>&1
if %errorlevel% neq 0 (
echo ❌ 错误:未检测到 curl 命令
echo 📥 请确保 curl 已安装并添加到 PATH
pause
exit /b 1
)
REM 检查网关服务是否运行
echo 🔍 检查网关服务状态...
curl -s -o nul -w "%%{http_code}" http://localhost:8000/api/v1/health > temp_status.txt
set /p STATUS_CODE=<temp_status.txt
del temp_status.txt
if "%STATUS_CODE%"=="200" (
echo ✅ 网关服务运行正常 (状态码: %STATUS_CODE%)
) else (
echo ⚠️ 警告:网关服务可能未运行 (状态码: %STATUS_CODE%)
echo 🔧 请先启动网关服务
)
REM 测试 WebSocket 连接
echo 🌐 测试 WebSocket 连接...
echo 📡 连接到 %GATEWAY_URL%/ws ...
REM 使用 curl 测试 WebSocket 握手
curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Sec-WebSocket-Version: 13" -H "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==" --max-time %TEST_TIMEOUT% %GATEWAY_URL%/ws > temp_websocket_test.txt 2>&1
REM 检查响应
findstr "HTTP/1.1 101" temp_websocket_test.txt >nul
if %errorlevel%==0 (
echo ✅ WebSocket 连接测试成功!
echo 📊 响应详情:
type temp_websocket_test.txt
) else (
echo ❌ WebSocket 连接测试失败
echo 🔍 错误详情:
type temp_websocket_test.txt
)
del temp_websocket_test.txt 2>nul
REM 测试 Nginx 代理的 WebSocket
echo.
echo 🔄 测试 Nginx WebSocket 代理...
set NGINX_WS_URL=ws://localhost/ws
echo 📡 连接到 %NGINX_WS_URL% ...
curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Sec-WebSocket-Version: 13" -H "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==" --max-time %TEST_TIMEOUT% %NGINX_WS_URL% > temp_nginx_test.txt 2>&1
findstr "HTTP/1.1 101" temp_nginx_test.txt >nul
if %errorlevel%==0 (
echo ✅ Nginx WebSocket 代理测试成功!
echo 📊 响应详情:
type temp_nginx_test.txt
) else (
echo ❌ Nginx WebSocket 代理测试失败
echo 🔍 错误详情:
type temp_nginx_test.txt
)
del temp_nginx_test.txt 2>nul
REM 测试 SmartClaw WebSocket 客户端连接
echo.
echo 🤖 测试 SmartClaw WebSocket 客户端连接...
echo 🔍 检查 SmartClaw 服务日志...
echo 📍 请检查 SmartClaw 控制台输出中的 WebSocket 连接状态
echo 📄 日志应包含 "WebSocket 客户端连接成功" 或类似信息
REM 提供手动测试命令
echo.
echo 🛠️ 手动测试命令:
echo 测试网关 WebSocket: curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Sec-WebSocket-Version: 13" -H "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==" ws://localhost:8000/ws
echo 测试 Nginx 代理: curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Sec-WebSocket-Version: 13" -H "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==" ws://localhost/ws
echo 检查网关健康: curl http://localhost:8000/api/v1/health
echo 检查 Nginx 状态: curl http://localhost/api/v1/health
echo.
echo 📊 测试结果摘要:
echo 网关服务状态: %STATUS_CODE%
echo WebSocket 直连: 见上方结果
echo Nginx WebSocket 代理: 见上方结果
echo.
echo 🎯 下一步:
echo 1. 如果测试失败,请检查服务是否启动
echo 2. 检查防火墙设置是否允许端口 8000 和 80/443
echo 3. 查看服务日志了解详细错误信息
echo 4. 确保 Nginx 配置正确并重载配置
echo.
echo 📅 测试完成时间: %date% %time%
pause