sse iphone fix 2
This commit is contained in:
@@ -737,12 +737,27 @@
|
||||
|
||||
// API_BASE injected from backend (supports Docker/K8s environments)
|
||||
window.API_BASE = '{{ api_base }}';
|
||||
window.RUNTIME_CONFIG = window.RUNTIME_CONFIG || {};
|
||||
|
||||
// Helper function to construct API URLs
|
||||
function api(url) {
|
||||
return `${window.API_BASE}${url}`;
|
||||
}
|
||||
|
||||
// iOS/Safari Polyfill laden (nur wenn nötig)
|
||||
(function() {
|
||||
var isIOS = /iP(hone|od|ad)/.test(navigator.platform) ||
|
||||
(navigator.userAgent.includes("Mac") && "ontouchend" in document);
|
||||
if (isIOS && typeof window.EventSourcePolyfill === "undefined") {
|
||||
var s = document.createElement("script");
|
||||
s.src = "https://cdn.jsdelivr.net/npm/event-source-polyfill@1.0.31/src/eventsource.min.js";
|
||||
s.onerror = function() {
|
||||
console.warn("EventSource polyfill konnte nicht geladen werden");
|
||||
};
|
||||
document.head.appendChild(s);
|
||||
}
|
||||
})();
|
||||
|
||||
let eventSource = null;
|
||||
let currentState = {};
|
||||
let thermostatTargets = {};
|
||||
@@ -999,35 +1014,12 @@
|
||||
}
|
||||
}
|
||||
|
||||
// Connect to SSE
|
||||
let reconnectAttempts = 0;
|
||||
const maxReconnectDelay = 30000; // Max 30 seconds
|
||||
// Safari/iOS-kompatibler SSE Client mit Auto-Reconnect
|
||||
let reconnectDelay = 2500;
|
||||
let reconnectTimer = null;
|
||||
|
||||
function connectSSE() {
|
||||
// Close existing connection if any
|
||||
if (eventSource) {
|
||||
try {
|
||||
eventSource.close();
|
||||
} catch (e) {
|
||||
console.error('Error closing EventSource:', e);
|
||||
}
|
||||
eventSource = null;
|
||||
}
|
||||
|
||||
console.log(`Connecting to SSE... (attempt ${reconnectAttempts + 1})`);
|
||||
|
||||
try {
|
||||
eventSource = new EventSource(api('/realtime'));
|
||||
|
||||
eventSource.onopen = () => {
|
||||
console.log('SSE connected successfully');
|
||||
reconnectAttempts = 0; // Reset counter on successful connection
|
||||
document.getElementById('connection-status').textContent = 'Verbunden';
|
||||
document.getElementById('connection-status').className = 'status connected';
|
||||
};
|
||||
|
||||
eventSource.addEventListener('message', (e) => {
|
||||
const data = JSON.parse(e.data);
|
||||
// Global handleSSE function für SSE-Nachrichten
|
||||
window.handleSSE = function(data) {
|
||||
console.log('SSE message:', data);
|
||||
|
||||
addEvent(data);
|
||||
@@ -1066,80 +1058,104 @@
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
eventSource.addEventListener('ping', (e) => {
|
||||
console.log('Heartbeat received');
|
||||
});
|
||||
|
||||
eventSource.onerror = (error) => {
|
||||
console.error('SSE error:', error, 'readyState:', eventSource?.readyState);
|
||||
document.getElementById('connection-status').textContent = 'Getrennt';
|
||||
document.getElementById('connection-status').className = 'status disconnected';
|
||||
};
|
||||
|
||||
function cleanupSSE() {
|
||||
if (eventSource) {
|
||||
try {
|
||||
eventSource.close();
|
||||
} catch(e) {
|
||||
console.error('Error closing EventSource on error:', e);
|
||||
console.error('Error closing EventSource:', e);
|
||||
}
|
||||
eventSource = null;
|
||||
}
|
||||
if (reconnectTimer) {
|
||||
clearTimeout(reconnectTimer);
|
||||
reconnectTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Exponential backoff with max delay
|
||||
reconnectAttempts++;
|
||||
const delay = Math.min(
|
||||
1000 * Math.pow(2, reconnectAttempts - 1),
|
||||
maxReconnectDelay
|
||||
);
|
||||
function scheduleReconnect() {
|
||||
if (reconnectTimer) return;
|
||||
console.log(`Reconnecting in ${reconnectDelay}ms...`);
|
||||
reconnectTimer = setTimeout(() => {
|
||||
reconnectTimer = null;
|
||||
connectSSE();
|
||||
// Backoff bis 10s
|
||||
reconnectDelay = Math.min(reconnectDelay * 2, 10000);
|
||||
}, reconnectDelay);
|
||||
}
|
||||
|
||||
console.log(`Reconnecting in ${delay}ms... (attempt ${reconnectAttempts})`);
|
||||
setTimeout(connectSSE, delay);
|
||||
function connectSSE() {
|
||||
cleanupSSE();
|
||||
|
||||
const REALTIME_URL = (window.RUNTIME_CONFIG && window.RUNTIME_CONFIG.REALTIME_URL)
|
||||
? window.RUNTIME_CONFIG.REALTIME_URL
|
||||
: api('/realtime');
|
||||
|
||||
console.log('Connecting to SSE:', REALTIME_URL);
|
||||
|
||||
try {
|
||||
// Verwende Polyfill wenn verfügbar, sonst native EventSource
|
||||
const EventSourceImpl = window.EventSourcePolyfill || window.EventSource;
|
||||
eventSource = new EventSourceImpl(REALTIME_URL, {
|
||||
withCredentials: false
|
||||
});
|
||||
|
||||
eventSource.onopen = function() {
|
||||
console.log('SSE connected successfully');
|
||||
reconnectDelay = 2500; // Reset backoff
|
||||
document.getElementById('connection-status').textContent = 'Verbunden';
|
||||
document.getElementById('connection-status').className = 'status connected';
|
||||
};
|
||||
|
||||
eventSource.onmessage = function(evt) {
|
||||
if (!evt || !evt.data) return;
|
||||
|
||||
// Heartbeats beginnen mit ":" -> ignorieren
|
||||
if (typeof evt.data === "string" && evt.data.charAt(0) === ":") {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const data = JSON.parse(evt.data);
|
||||
if (window.handleSSE) {
|
||||
window.handleSSE(data);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error parsing SSE message:', e);
|
||||
}
|
||||
};
|
||||
|
||||
eventSource.onerror = function(error) {
|
||||
console.error('SSE error:', error, 'readyState:', eventSource?.readyState);
|
||||
document.getElementById('connection-status').textContent = 'Getrennt';
|
||||
document.getElementById('connection-status').className = 'status disconnected';
|
||||
|
||||
// Safari/iOS verliert Netz beim App-Switch: ruhig reconnecten
|
||||
scheduleReconnect();
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
console.error('Failed to create EventSource:', error);
|
||||
document.getElementById('connection-status').textContent = 'Getrennt';
|
||||
document.getElementById('connection-status').className = 'status disconnected';
|
||||
|
||||
reconnectAttempts++;
|
||||
const delay = Math.min(
|
||||
1000 * Math.pow(2, reconnectAttempts - 1),
|
||||
maxReconnectDelay
|
||||
);
|
||||
|
||||
setTimeout(connectSSE, delay);
|
||||
scheduleReconnect();
|
||||
}
|
||||
}
|
||||
|
||||
// Safari/iOS specific: Reconnect when page becomes visible
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (document.visibilityState === 'visible') {
|
||||
console.log('Page visible, 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
|
||||
// Visibility-Change Handler für iOS App-Switch
|
||||
document.addEventListener('visibilitychange', function() {
|
||||
if (!document.hidden) {
|
||||
// Wenn wieder sichtbar & keine offene Verbindung: verbinden
|
||||
if (!eventSource || eventSource.readyState !== 1) {
|
||||
console.log('Page visible again, reconnecting SSE...');
|
||||
connectSSE();
|
||||
} else {
|
||||
console.log('SSE connection OK, readyState:', eventSource.readyState);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 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);
|
||||
}
|
||||
});
|
||||
|
||||
// Initialize
|
||||
// Start SSE connection
|
||||
connectSSE();
|
||||
|
||||
// Load initial device states
|
||||
|
||||
Reference in New Issue
Block a user