This commit is contained in:
2025-11-08 17:36:52 +01:00
parent 4b196c1278
commit acb5e0a209
2 changed files with 34 additions and 35 deletions

View File

@@ -855,6 +855,12 @@
// Update device state
if (data.type === 'state' && data.device_id && data.payload) {
const card = document.querySelector(`[data-device-id="${data.device_id}"]`);
if (!card) {
console.warn(`No card found for device ${data.device_id}`);
return;
}
// Check if it's a light
if (data.payload.power !== undefined) {
currentState[data.device_id] = data.payload.power;
@@ -930,11 +936,13 @@
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') {
console.log('Page visible, checking SSE connection...');
// Force reconnect if connection is dead
if (!eventSource || eventSource.readyState === EventSource.CLOSED || eventSource.readyState === EventSource.CONNECTING) {
console.log('SSE connection dead or connecting, forcing reconnect...');
// Only reconnect if connection is actually dead (CLOSED = 2)
if (!eventSource || eventSource.readyState === EventSource.CLOSED) {
console.log('SSE connection dead, forcing reconnect...');
reconnectAttempts = 0; // Reset for immediate reconnect
connectSSE();
} else {
console.log('SSE connection OK, readyState:', eventSource.readyState);
}
}
});
@@ -942,26 +950,16 @@
// Safari/iOS specific: Reconnect on page focus
window.addEventListener('focus', () => {
console.log('Window focused, checking SSE connection...');
// Only reconnect if connection is actually dead (CLOSED = 2)
if (!eventSource || eventSource.readyState === EventSource.CLOSED) {
console.log('SSE connection dead, forcing reconnect...');
reconnectAttempts = 0; // Reset for immediate reconnect
connectSSE();
} else {
console.log('SSE connection OK, readyState:', eventSource.readyState);
}
});
// Safari/iOS specific: Keep connection alive with periodic check
setInterval(() => {
if (!eventSource || eventSource.readyState === EventSource.CLOSED) {
console.log('Periodic check: SSE connection dead, reconnecting...');
reconnectAttempts = 0;
connectSSE();
} else if (eventSource.readyState === EventSource.CONNECTING) {
console.log('Periodic check: SSE still connecting, readyState:', eventSource.readyState);
} else {
console.log('Periodic check: SSE connection alive, readyState:', eventSource.readyState);
}
}, 10000); // Check every 10 seconds
// Initialize
connectSSE();