Add heartbeat timeout to detect server disconnection
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1576,8 +1576,19 @@
|
|||||||
|
|
||||||
function connectSSE() {
|
function connectSSE() {
|
||||||
const evtSource = new EventSource('/api/status/stream');
|
const evtSource = new EventSource('/api/status/stream');
|
||||||
|
let heartbeatTimeout = null;
|
||||||
|
|
||||||
|
function resetHeartbeat() {
|
||||||
|
if (heartbeatTimeout) clearTimeout(heartbeatTimeout);
|
||||||
|
heartbeatTimeout = setTimeout(() => {
|
||||||
|
setConnectionStatus(false);
|
||||||
|
evtSource.close();
|
||||||
|
setTimeout(connectSSE, 2000);
|
||||||
|
}, 10000);
|
||||||
|
}
|
||||||
|
|
||||||
evtSource.addEventListener('status', async (event) => {
|
evtSource.addEventListener('status', async (event) => {
|
||||||
|
resetHeartbeat();
|
||||||
const data = JSON.parse(event.data);
|
const data = JSON.parse(event.data);
|
||||||
if (!currentConfig) {
|
if (!currentConfig) {
|
||||||
const configResp = await fetch('/api/config');
|
const configResp = await fetch('/api/config');
|
||||||
@@ -1588,9 +1599,11 @@
|
|||||||
|
|
||||||
evtSource.onopen = () => {
|
evtSource.onopen = () => {
|
||||||
setConnectionStatus(true);
|
setConnectionStatus(true);
|
||||||
|
resetHeartbeat();
|
||||||
};
|
};
|
||||||
|
|
||||||
evtSource.onerror = () => {
|
evtSource.onerror = () => {
|
||||||
|
if (heartbeatTimeout) clearTimeout(heartbeatTimeout);
|
||||||
setConnectionStatus(false);
|
setConnectionStatus(false);
|
||||||
evtSource.close();
|
evtSource.close();
|
||||||
setTimeout(connectSSE, 2000);
|
setTimeout(connectSSE, 2000);
|
||||||
|
|||||||
Reference in New Issue
Block a user