From 6d4e2dfe15b07f2633fb973a331d1bade121164e Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Thu, 7 May 2026 08:52:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E4=BF=9D=E6=B4=BB=E6=9C=BA=E5=88=B6):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9=E8=AF=9D=E4=BF=9D=E6=B4=BB=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E4=BB=A5=E7=BB=B4=E6=8C=81=E4=BA=A4=E4=BA=92=E7=8A=B6?= =?UTF-8?q?=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实现自动重新激活交互模式的功能,当任务结束时若保活激活则自动重新连接 添加定时保活ping机制,定期激活交互模式防止连接断开 在SDK初始化和销毁时自动管理保活定时器 --- frontend/scripts/main.js | 50 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/frontend/scripts/main.js b/frontend/scripts/main.js index ada4933..01a9241 100644 --- a/frontend/scripts/main.js +++ b/frontend/scripts/main.js @@ -43,7 +43,8 @@ const state = { isSpeaking: false, isMuted: false, jobInfo: null, - currentInteractionMode: InteractionMode.AUDIO + currentInteractionMode: InteractionMode.AUDIO, + keepAliveActive: false }; const elements = { @@ -256,14 +257,54 @@ const eventListeners = { }, [ICSEventKey.JOB_END]: (data) => { - logger.info('任务已结束', data); + logger.info('任务已结束,正在自动重新激活...', data); state.isChatting = false; state.jobInfo = null; - updateConnectionStatus(false); + if (state.keepAliveActive) { + setTimeout(async () => { + try { + logger.info('正在调用 activeInteractionMode...'); + await HwICSUiSdk.activeInteractionMode(); + logger.info('自动激活成功'); + state.isChatting = true; + updateUIState(); + } catch (error) { + logger.error('自动激活失败', error); + } + }, 1000); + } updateUIState(); } }; +let keepAliveTimer = null; +const KEEP_ALIVE_INTERVAL = 30000; + +function startKeepAlive() { + if (keepAliveTimer) return; + state.keepAliveActive = true; + logger.info('启动对话保活机制'); + keepAliveTimer = setInterval(async () => { + if (state.isInitialized && state.keepAliveActive) { + try { + await HwICSUiSdk.activeInteractionMode(); + logger.debug('保活 ping'); + } catch (error) { + logger.warn('保活失败', error); + } + } + }, KEEP_ALIVE_INTERVAL); +} + +function stopKeepAlive() { + if (keepAliveTimer) { + clearInterval(keepAliveTimer); + keepAliveTimer = null; + } + state.keepAliveActive = false; + logger.info('停止对话保活机制'); +} + function updateUIState() { const { isInitialized } = state; @@ -442,6 +483,7 @@ async function initializeSDK() { fullscreenState.sdkInitialized = true; updateConnectionStatus(true); updateUIState(); + startKeepAlive(); elements.sdkVersion.textContent = `v${HwICSUiSdk.version}`; @@ -468,6 +510,7 @@ async function destroySDK() { state.isSpeaking = false; state.jobInfo = null; fullscreenState.sdkInitialized = false; + stopKeepAlive(); updateConnectionStatus(false); updateUIState(); @@ -1284,6 +1327,7 @@ function bindEvents() { window.addEventListener('beforeunload', () => { if (state.isInitialized) { + stopKeepAlive(); HwICSUiSdk.destroy(); } });