feat(保活机制): 添加对话保活功能以维持交互状态
实现自动重新激活交互模式的功能,当任务结束时若保活激活则自动重新连接 添加定时保活ping机制,定期激活交互模式防止连接断开 在SDK初始化和销毁时自动管理保活定时器
This commit is contained in:
@@ -43,7 +43,8 @@ const state = {
|
|||||||
isSpeaking: false,
|
isSpeaking: false,
|
||||||
isMuted: false,
|
isMuted: false,
|
||||||
jobInfo: null,
|
jobInfo: null,
|
||||||
currentInteractionMode: InteractionMode.AUDIO
|
currentInteractionMode: InteractionMode.AUDIO,
|
||||||
|
keepAliveActive: false
|
||||||
};
|
};
|
||||||
|
|
||||||
const elements = {
|
const elements = {
|
||||||
@@ -256,14 +257,54 @@ const eventListeners = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
[ICSEventKey.JOB_END]: (data) => {
|
[ICSEventKey.JOB_END]: (data) => {
|
||||||
logger.info('任务已结束', data);
|
logger.info('任务已结束,正在自动重新激活...', data);
|
||||||
state.isChatting = false;
|
state.isChatting = false;
|
||||||
state.jobInfo = null;
|
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();
|
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() {
|
function updateUIState() {
|
||||||
const { isInitialized } = state;
|
const { isInitialized } = state;
|
||||||
|
|
||||||
@@ -442,6 +483,7 @@ async function initializeSDK() {
|
|||||||
fullscreenState.sdkInitialized = true;
|
fullscreenState.sdkInitialized = true;
|
||||||
updateConnectionStatus(true);
|
updateConnectionStatus(true);
|
||||||
updateUIState();
|
updateUIState();
|
||||||
|
startKeepAlive();
|
||||||
|
|
||||||
elements.sdkVersion.textContent = `v${HwICSUiSdk.version}`;
|
elements.sdkVersion.textContent = `v${HwICSUiSdk.version}`;
|
||||||
|
|
||||||
@@ -468,6 +510,7 @@ async function destroySDK() {
|
|||||||
state.isSpeaking = false;
|
state.isSpeaking = false;
|
||||||
state.jobInfo = null;
|
state.jobInfo = null;
|
||||||
fullscreenState.sdkInitialized = false;
|
fullscreenState.sdkInitialized = false;
|
||||||
|
stopKeepAlive();
|
||||||
|
|
||||||
updateConnectionStatus(false);
|
updateConnectionStatus(false);
|
||||||
updateUIState();
|
updateUIState();
|
||||||
@@ -1284,6 +1327,7 @@ function bindEvents() {
|
|||||||
|
|
||||||
window.addEventListener('beforeunload', () => {
|
window.addEventListener('beforeunload', () => {
|
||||||
if (state.isInitialized) {
|
if (state.isInitialized) {
|
||||||
|
stopKeepAlive();
|
||||||
HwICSUiSdk.destroy();
|
HwICSUiSdk.destroy();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user