fix: 修复重复请求ota与重复建立连接问题

This commit is contained in:
lww155
2026-01-22 15:54:26 +08:00
parent 43ddc0135c
commit 9d9ca1475d
3 changed files with 11 additions and 60 deletions
+9 -45
View File
@@ -1,5 +1,5 @@
// UI控制模块
import { loadConfig, saveConfig, getConfig } from '../config/manager.js';
import { loadConfig, saveConfig } from '../config/manager.js';
import { getAudioRecorder } from '../core/audio/recorder.js';
import { getWebSocketHandler } from '../core/network/websocket.js';
import { getAudioPlayer } from '../core/audio/player.js';
@@ -11,7 +11,6 @@ class UIController {
this.visualizerCanvas = null;
this.visualizerContext = null;
this.audioStatsTimer = null;
this.wsTimer = null;
this.currentBackgroundIndex = 0;
this.backgroundImages = ['1.png', '2.png', '3.png'];
@@ -93,6 +92,7 @@ class UIController {
if (isConnected) {
wsHandler.disconnect();
this.updateDialButton(false);
this.addChatMessage('已断开连接,期待下次再见~😉', false);
} else {
// 检查OTA地址是否已填写
const otaUrlInput = document.getElementById('otaUrl');
@@ -347,30 +347,9 @@ class UIController {
this.addChatMessage('配置已保存', false);
}
// 处理未绑定设备事件
handleUnboundDevice() {
const dialBtn = document.getElementById('dialBtn');
if (dialBtn) {
dialBtn.click();
}
}
// 拨号成功后直接开始录音
dialAndRecord() {
const recordBtn = document.getElementById('recordBtn');
const wsHandler = getWebSocketHandler();
this.wsTimer = setInterval(() => {
if (wsHandler.isConnected() && wsHandler.websocket.onopen) {
clearInterval(this.wsTimer);
this.wsTimer = null;
if (wsHandler.isEstablishConnection) {
recordBtn.click();
} else {
this.handleUnboundDevice();
}
return;
}
}, 500);
// 连接成功后开始对话
startAIChatSession() {
this.addChatMessage('连接成功,开始聊天吧~🙂', false);
}
// 处理连接按钮点击
@@ -412,21 +391,12 @@ class UIController {
}
try {
// 获取配置信息
const config = getConfig();
// 导入OTA连接
const { webSocketConnect } = await import('../core/network/ota-connector.js');
// 获取WebSocket处理
const wsHandler = getWebSocketHandler();
const isConnected = await wsHandler.connect();
// 建立OTA连接
const websocket = await webSocketConnect(otaUrl, config);
if (websocket) {
// 获取WebSocket处理器
const wsHandler = getWebSocketHandler();
// 设置WebSocket连接
wsHandler.websocket = websocket;
if (isConnected) {
// 设置连接状态回调
wsHandler.onConnectionStateChange = (isConnected) => {
@@ -462,16 +432,10 @@ class UIController {
dialBtn.disabled = false;
dialBtn.querySelector('.btn-text').textContent = '挂断';
dialBtn.classList.add('dial-active');
this.dialAndRecord();
}
this.hideModal('settingsModal');
// 自动尝试建立WebSocket连接
setTimeout(() => {
wsHandler.connect();
}, 1000);
} else {
throw new Error('OTA连接失败');
}