我正在使用quickstart template进行多设备对话,并且当参与者断开连接时,似乎未触发参与者更改事件处理程序(participantsChanged
)。我希望会为参与者关闭浏览器窗口或失去互联网连接而获得LeftConversation
,但似乎仅当参与者选择断开连接时才触发该事件。
如果参与者通过单击“离开对话”按钮干净地离开对话,则将立即触发SpeechSDK.ParticipantChangedReason.LeftConversation
事件。
[如果参与者通过其他方法(例如,关闭浏览器窗口或单击浏览器的后退按钮)离开会话,则将在基础WebSocket中立即触发“ DisconnectSession”消息。这将在6分钟内提升为SpeechSDK.ParticipantChangedReason.LeftConversation
事件。在Javascript SDK中,websocket的“ DisconnectSession”消息当前未作为SDK事件公开。
作为一种解决方法,一种可能性是更新快速入门代码,以为浏览器的“ beforeunload”或“ unload”事件添加侦听器,该事件将代表参与者调用离开对话功能。
https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_eventhttps://developer.mozilla.org/en-US/docs/Web/API/Window/unload_event
示例代码:
document.addEventListener("DOMContentLoaded", function () {
// ... existing variable declarations
window.addEventListener('beforeunload', (event) => {
// Call LeaveConversation on the participant's behalf
handleLeaveConversation();
// Cancel the event as stated by the standard.
event.preventDefault();
// Chrome requires returnValue to be set.
event.returnValue = '';
});
// existing code ...