Ensure we close the active connection when opening another in the client
authorRaphaël Jakse <raphael.trivabble@jakse.fr>
Tue, 14 Apr 2020 18:37:45 +0000 (20:37 +0200)
committerRaphaël Jakse <raphael.trivabble@jakse.fr>
Tue, 14 Apr 2020 18:38:04 +0000 (20:38 +0200)
trivabble.js

index 3dc1dbae872bdd43f1abdcc3a4b73b9c2b13dc59..ff14bd9ad57b8a57159811a974d3149f459458f1 100644 (file)
         }
     }
 
-    function stopConnection() {
+    function closeConnections() {
         if (eventSource) {
             eventSource.onerror = null;
             eventSource.close();
             webSocket.close();
             webSocket = null;
         }
+    }
+
+    function stopConnection() {
+        closeConnections();
 
         if (retryPollingTimeout) {
             clearTimeout(retryPollingTimeout);
 
     function pollServerWithEventSource() {
         if (ENABLE_EVENT_SOURCE && window.EventSource) {
+            closeConnections();
             eventSource = new EventSource("/:trivabble/sse/" + JSON.stringify(cmdsWithContext()));
             bindConnectionEvents(eventSource);
             return;
 
     function pollServerWithWebSocket() {
         if (ENABLE_WEBSOCKETS && !blacklistWebsockets && window.WebSocket) {
+            closeConnections();
             webSocket = new WebSocket(
                 (window.location.protocol === "http:" ? "ws://" : "wss://") +
                 window.location.host +