Block WebSockets if we see that they don't work
authorRaphaël Jakse <raphael.trivabble@jakse.fr>
Mon, 13 Apr 2020 09:36:22 +0000 (11:36 +0200)
committerRaphaël Jakse <raphael.trivabble@jakse.fr>
Mon, 13 Apr 2020 09:36:22 +0000 (11:36 +0200)
trivabble.js

index 1262ac19556d3e9d83ce65724fdad4dd65c06516..d5a644f9f244a3dbf91fc42ee55ad894258af0f0 100644 (file)
@@ -31,6 +31,7 @@
     const POLLING_DELAY = 2000;
     const ENABLE_WEBSOCKETS = true;
     const ENABLE_EVENT_SOURCE = true;
+    const MAX_WEBSOCKET_ERRORS = 2;
 
     const _ = (window.libD && libD.l10n) ? libD.l10n() : function (s) {return s;};
 
     }
 
     let blacklistWebsockets = false;
+    let webSocketErrors = 0;
 
     function bindConnectionEvents(connection) {
         connection.onopen = connectionReady;
 
         connection.onmessage = function (e) {
+            if (connection === webSocket) {
+                webSocketErrors = 0;
+            }
+
             connectionReady();
             parseAndHandleReceivedData(e.data);
         };
 
         connection.onerror = function (e) {
             console.error("Connection error:", e);
+
             if (connection === eventSource) {
                 retryPolling();
+            } else {
+                webSocketErrors++;
+
+                if (webSocketErrors > MAX_WEBSOCKET_ERRORS) {
+                    blacklistWebsockets = true;
+                }
             }
         };