diff --git a/lex-web-ui/src/store/actions.js b/lex-web-ui/src/store/actions.js index bf71cfd4..6c207a97 100644 --- a/lex-web-ui/src/store/actions.js +++ b/lex-web-ui/src/store/actions.js @@ -710,11 +710,11 @@ export default { wsClient.onmessage = (event) => { if(event.data!=='/stop/' && context.getters.isStartingTypingWsMessages()){ - console.info("streaming ", context.getters.isStartingTypingWsMessages()); + console.info('Streaming: ', context.getters.isStartingTypingWsMessages()); context.commit('pushWebSocketMessage',event.data); context.dispatch('typingWsMessages') }else{ - console.info('stopping streaming'); + console.info('Currently not streaming'); } } } @@ -1304,6 +1304,41 @@ export default { const signedUrl = Signer.signUrl(context.state.config.lex.streamingWebSocketEndpoint+'?sessionId='+sessionId, accessInfo, serviceInfo); wsClient = new WebSocket(signedUrl); + + // Add heartbeat logic + const HEARTBEAT_INTERVAL = 540000; // 9 minutes + const MAX_DURATION = 7200000; // 2 hours + const startTime = Date.now(); + let heartbeatTimer = null; + + function startHeartbeat() { + if (wsClient.readyState === WebSocket.OPEN) { + const elapsedTime = Date.now() - startTime; + if (elapsedTime < MAX_DURATION) { + const pingMessage = JSON.stringify({ action: 'ping' }); + wsClient.send(pingMessage); + console.log('Sending Ping:', new Date().toISOString()); + heartbeatTimer = setTimeout(startHeartbeat, HEARTBEAT_INTERVAL); + } else { + console.log('Stopped sending pings after reaching 2-hour limit.'); + clearTimeout(heartbeatTimer); + } + } + } + wsClient.onopen = () => { + console.log('WebSocket Connected'); + startHeartbeat(); + }; + + wsClient.onclose = () => { + console.log('WebSocket Closed'); + clearTimeout(heartbeatTimer); + }; + + wsClient.onerror = (error) => { + console.log('WebSocket Error', error.message); + clearTimeout(heartbeatTimer); + }; }, typingWsMessages(context){ if (context.getters.wsMessagesCurrentIndex()