From 89e299764dc0ec2754b2d201a219bb2e5d7c913c Mon Sep 17 00:00:00 2001 From: Chingfeng Li Date: Fri, 17 Oct 2025 18:06:22 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=B5=8B=E8=AF=95=E8=AE=A4?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/test/js/xiaoZhiConnect.js | 5 +- main/xiaozhi-server/test/test_page.html | 101 ++++-------------- 2 files changed, 27 insertions(+), 79 deletions(-) diff --git a/main/xiaozhi-server/test/js/xiaoZhiConnect.js b/main/xiaozhi-server/test/js/xiaoZhiConnect.js index 4ae5a71f..90efe8d7 100644 --- a/main/xiaozhi-server/test/js/xiaoZhiConnect.js +++ b/main/xiaozhi-server/test/js/xiaoZhiConnect.js @@ -10,7 +10,10 @@ export async function webSocketConnect(otaUrl, config){ // 发送OTA请求并获取返回的websocket信息 const otaResult = await sendOTA(otaUrl, config); - if (!otaResult) return; + if (!otaResult) { + log('无法从OTA服务器获取信息', 'error'); + return; + } // 从OTA响应中提取websocket信息 const { websocket } = otaResult; diff --git a/main/xiaozhi-server/test/test_page.html b/main/xiaozhi-server/test/test_page.html index 5c14327c..ad4eb71a 100644 --- a/main/xiaozhi-server/test/test_page.html +++ b/main/xiaozhi-server/test/test_page.html @@ -1092,6 +1092,9 @@ const config = getConfig(); + const otaUrl = document.getElementById('otaUrl').value.trim(); + + // 显示服务器配置 log('-------- 服务器认证配置检查 --------', 'info'); log('请确认config.yaml中的auth配置:', 'info'); @@ -1099,90 +1102,32 @@ log('2. 如果启用了认证,请确认使用了正确的token', 'info'); log(`3. 或者在allowed_devices中添加了测试设备MAC:${config.deviceMac}`, 'info'); - const serverUrl = serverUrlInput.value.trim(); - if (!serverUrl) { - log('请输入服务器地址', 'error'); - return; - } - - // 测试连接 - log('尝试不同认证参数的连接:', 'info'); - - // 测试1: 无参数连接 try { - log('测试1: 尝试无参数连接...', 'info'); - const ws1 = new WebSocket(serverUrl); + log('尝试连接...', 'info'); + const ws = await webSocketConnect(otaUrl, config) - ws1.onopen = () => { - log('测试1成功: 无参数可连接,服务器可能没有启用认证', 'success'); - ws1.close(); - }; - - ws1.onerror = (error) => { - log('测试1失败: 无参数连接被拒绝,服务器可能启用了认证', 'error'); - }; - - // 5秒后关闭测试连接 - setTimeout(() => { - if (ws1.readyState === WebSocket.CONNECTING || ws1.readyState === WebSocket.OPEN) { - ws1.close(); - } - }, 5000); - } catch (error) { - log(`测试1出错: ${error.message}`, 'error'); - } - - // 测试2: 带参数连接 - setTimeout(async () => { - try { - log('测试2: 尝试带token参数连接...', 'info'); - - let url = new URL(serverUrl); - url.searchParams.append('token', config.token); - url.searchParams.append('device_id', config.deviceId); - url.searchParams.append('device_mac', config.deviceMac); - - const ws2 = new WebSocket(url.toString()); - - ws2.onopen = () => { - log('测试2成功: 带token参数可连接', 'success'); - - // 尝试发送hello消息 - const helloMsg = { - type: 'hello', - device_id: config.deviceId, - device_mac: config.deviceMac, - token: config.token - }; - - ws2.send(JSON.stringify(helloMsg)); - log('已发送hello测试消息', 'info'); - - // 监听响应 - ws2.onmessage = (event) => { - try { - const response = JSON.parse(event.data); - if (response.type === 'hello' && response.session_id) { - log(`测试完全成功! 收到hello响应,会话ID: ${response.session_id}`, 'success'); - ws2.close(); - } - } catch (e) { - log(`收到非JSON响应: ${event.data}`, 'info'); - } - }; - - // 5秒后关闭 - setTimeout(() => ws2.close(), 5000); + if (ws) { + ws.onopen = () => { + log('测试成功: 可连接,服务器可能没有启用认证', 'success'); + ws.close(); }; - ws2.onerror = (error) => { - log('测试2失败: 带token参数连接被拒绝', 'error'); - log('请检查token是否正确,或服务器是否接受URL参数认证', 'error'); + ws.onerror = (error) => { + log('测试失败: 连接被拒绝,服务器可能启用了认证', 'error'); }; - } catch (error) { - log(`测试2出错: ${error.message}`, 'error'); + + // 5秒后关闭测试连接 + setTimeout(() => { + if (ws.readyState === WebSocket.CONNECTING || ws.readyState === WebSocket.OPEN) { + ws.close(); + } + }, 5000); + } else { + log(`测试失败: 连接被拒绝,服务器可能启用了认证`, 'error'); } - }, 6000); + } catch (error) { + log(`测试出错: ${error.message}`, 'error'); + } log('认证测试已启动,请查看测试结果...', 'info'); }