mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-28 10:03:54 +08:00
更新test测试页面,使用ota返回的连接信息和认证信息
This commit is contained in:
@@ -2,23 +2,46 @@ import { log } from './utils/logger.js';
|
|||||||
import { otaStatusStyle } from './document.js'
|
import { otaStatusStyle } from './document.js'
|
||||||
|
|
||||||
// WebSocket 连接
|
// WebSocket 连接
|
||||||
export async function webSocketConnect(otaUrl,wsUrl,config){
|
export async function webSocketConnect(otaUrl, config){
|
||||||
if (!validateWsUrl(wsUrl)) {
|
|
||||||
return; // 直接返回,不再往下执行
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!validateConfig(config)) {
|
if (!validateConfig(config)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const ok = await sendOTA(otaUrl, config);
|
|
||||||
if (!ok) return;
|
|
||||||
|
|
||||||
// 使用自定义WebSocket实现以添加认证头信息
|
// 发送OTA请求并获取返回的websocket信息
|
||||||
let connUrl = new URL(wsUrl);
|
const otaResult = await sendOTA(otaUrl, config);
|
||||||
// 添加认证参数
|
if (!otaResult) return;
|
||||||
|
|
||||||
|
// 从OTA响应中提取websocket信息
|
||||||
|
const { websocket } = otaResult;
|
||||||
|
if (!websocket || !websocket.url || !websocket.token) {
|
||||||
|
log('OTA响应中缺少websocket信息', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用OTA返回的websocket URL
|
||||||
|
let connUrl = new URL(websocket.url);
|
||||||
|
|
||||||
|
// 添加token参数(从OTA响应中获取)
|
||||||
|
if (websocket.token) {
|
||||||
|
if(websocket.token.startsWith("Bearer ")){
|
||||||
|
connUrl.searchParams.append('authorization', websocket.token);
|
||||||
|
} else {
|
||||||
|
connUrl.searchParams.append('authorization','Bearer ' + websocket.token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加认证参数(保持原有逻辑)
|
||||||
connUrl.searchParams.append('device-id', config.deviceId);
|
connUrl.searchParams.append('device-id', config.deviceId);
|
||||||
connUrl.searchParams.append('client-id', config.clientId);
|
connUrl.searchParams.append('client-id', config.clientId);
|
||||||
log(`正在连接: ${connUrl.toString()}`, 'info');
|
|
||||||
|
const wsurl = connUrl.toString()
|
||||||
|
|
||||||
|
log(`正在连接: ${wsurl}`, 'info');
|
||||||
|
|
||||||
|
if (wsurl) {
|
||||||
|
document.getElementById('serverUrl').value = wsurl;
|
||||||
|
}
|
||||||
|
|
||||||
return new WebSocket(connUrl.toString());
|
return new WebSocket(connUrl.toString());
|
||||||
}
|
}
|
||||||
@@ -48,7 +71,7 @@ function validateWsUrl(wsUrl){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// OTA发送请求,验证状态
|
// OTA发送请求,验证状态,并返回响应数据
|
||||||
async function sendOTA(otaUrl, config) {
|
async function sendOTA(otaUrl, config) {
|
||||||
try {
|
try {
|
||||||
const res = await fetch(otaUrl, {
|
const res = await fetch(otaUrl, {
|
||||||
@@ -90,14 +113,9 @@ async function sendOTA(otaUrl, config) {
|
|||||||
|
|
||||||
const result = await res.json();
|
const result = await res.json();
|
||||||
otaStatusStyle(true)
|
otaStatusStyle(true)
|
||||||
return true; // 成功
|
return result; // 返回完整的响应数据
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
otaStatusStyle(false)
|
otaStatusStyle(false)
|
||||||
return false; // 失败
|
return null; // 失败返回null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -133,8 +133,8 @@
|
|||||||
<div class="connection-controls">
|
<div class="connection-controls">
|
||||||
<input type="text" id="otaUrl" value="http://127.0.0.1:8002/xiaozhi/ota/"
|
<input type="text" id="otaUrl" value="http://127.0.0.1:8002/xiaozhi/ota/"
|
||||||
placeholder="OTA服务器地址,如:http://127.0.0.1:8002/xiaozhi/ota/" />
|
placeholder="OTA服务器地址,如:http://127.0.0.1:8002/xiaozhi/ota/" />
|
||||||
<input type="text" id="serverUrl" value="ws://127.0.0.1:8000/xiaozhi/v1/"
|
<input type="text" id="serverUrl" value="" readonly disabled
|
||||||
placeholder="WebSocket服务器地址,如:ws://127.0.0.1:8000/xiaozhi/v1/" />
|
placeholder="自动从OTA接口获取" />
|
||||||
<button id="connectButton">连接</button>
|
<button id="connectButton">连接</button>
|
||||||
<button id="authTestButton">测试认证</button>
|
<button id="authTestButton">测试认证</button>
|
||||||
</div>
|
</div>
|
||||||
@@ -714,7 +714,7 @@
|
|||||||
localStorage.setItem('wsUrl', url);
|
localStorage.setItem('wsUrl', url);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const ws = await webSocketConnect(otaUrl, url, config)
|
const ws = await webSocketConnect(otaUrl, config)
|
||||||
if (ws === undefined) {
|
if (ws === undefined) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -756,7 +756,7 @@
|
|||||||
messageInput.disabled = true;
|
messageInput.disabled = true;
|
||||||
sendTextButton.disabled = true;
|
sendTextButton.disabled = true;
|
||||||
recordButton.disabled = true;
|
recordButton.disabled = true;
|
||||||
stopButton.disabled = true;
|
// stopButton.disabled = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
websocket.onerror = (error) => {
|
websocket.onerror = (error) => {
|
||||||
@@ -1049,11 +1049,6 @@
|
|||||||
document.getElementById('otaUrl').value = savedOtaUrl;
|
document.getElementById('otaUrl').value = savedOtaUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
const savedWsUrl = localStorage.getItem('wsUrl');
|
|
||||||
if (savedWsUrl) {
|
|
||||||
document.getElementById('serverUrl').value = savedWsUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 切换面板显示
|
// 切换面板显示
|
||||||
toggleButton.addEventListener('click', () => {
|
toggleButton.addEventListener('click', () => {
|
||||||
const isExpanded = configPanel.classList.contains('expanded');
|
const isExpanded = configPanel.classList.contains('expanded');
|
||||||
|
|||||||
Reference in New Issue
Block a user