From: Raphaƫl Jakse Date: Thu, 8 May 2025 09:09:37 +0000 (+0200) Subject: Better fallback handling when connecting X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=55f52ddb6857133fd6e766f4113e7127850c2957;p=trivabble.git Better fallback handling when connecting --- 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();