Add timeout after 10 minutes of inactivity on socket

This commit is contained in:
sabaimran 2024-04-02 22:12:27 +05:30
parent f48426623d
commit b4f71e06b3

View file

@ -48,6 +48,8 @@ To get started, just start typing below. You can also type / to see a list of co
});
}
var websocket = null;
var timeout = null;
var timeoutDuration = 600000; // 10 minutes
let region = null;
let city = null;
@ -861,12 +863,26 @@ To get started, just start typing below. You can also type / to see a list of co
rawResponse: "",
}
function resetTimeout() {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(function() {
if (websocket) {
websocket.close();
}
}, timeoutDuration);
}
if (chatBody.dataset.conversationId) {
webSocketUrl += `?conversation_id=${chatBody.dataset.conversationId}`;
webSocketUrl += `&region=${region}&city=${city}&country=${countryName}`;
websocket = new WebSocket(webSocketUrl);
websocket.onmessage = function(event) {
resetTimeout();
// Get the last element in the chat-body
let chunk = event.data;
if (chunk == "start_llm_response") {
@ -952,6 +968,8 @@ To get started, just start typing below. You can also type / to see a list of co
let greenDot = document.getElementById("connected-green-dot");
greenDot.style.display = "flex";
// Setup the timeout to close the connection after inactivity.
resetTimeout();
}
}