From 55f52ddb6857133fd6e766f4113e7127850c2957 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Jakse?= Date: Thu, 8 May 2025 11:09:37 +0200 Subject: [PATCH] Better fallback handling when connecting --- public/trivabble.js | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/public/trivabble.js b/public/trivabble.js index 04e023a..b1c5722 100644 --- a/public/trivabble.js +++ b/public/trivabble.js @@ -1431,10 +1431,14 @@ if (canConnect() && getSetting("ENABLE_EVENT_SOURCE") && window.EventSource) { closeConnections(); pollingServer = true; - - eventSource = new EventSource(getSetting("API_ENTRY_POINT") + "/sse/" + JSON.stringify(cmdsWithContext())); - bindConnectionEvents(eventSource); - return; + try { + eventSource = new EventSource(getSetting("API_ENTRY_POINT") + "/sse/" + JSON.stringify(cmdsWithContext())); + bindConnectionEvents(eventSource); + return; + } catch (e) { + pollingServer = false; + // failed, will fallback to XHR + } } pollServerWithXHR(); @@ -1444,16 +1448,19 @@ if (canConnect() && getSetting("ENABLE_WEBSOCKETS") && !blacklistWebsockets && window.WebSocket) { closeConnections(); pollingServer = true; - - webSocket = new WebSocket( - (window.location.protocol === "http:" ? "ws://" : "wss://") + - window.location.host + - getSetting("API_ENTRY_POINT") + "/ws/" + - JSON.stringify(cmdsWithContext()) - ); - - bindConnectionEvents(webSocket); - return; + try { + webSocket = new WebSocket( + (window.location.protocol === "http:" ? "ws://" : "wss://") + + window.location.host + + getSetting("API_ENTRY_POINT") + "/ws/" + + JSON.stringify(cmdsWithContext()) + ); + bindConnectionEvents(webSocket); + return; + } catch (e) { + // failed, will fall back to event source + pollingServer = false; + } } pollServerWithEventSource(); -- 2.30.2