]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/commitdiff
Try to reconnect the disconnected socket every 10 seconds
authorMichal Sojka <michal.sojka@cvut.cz>
Mon, 6 Aug 2018 14:39:28 +0000 (16:39 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Mon, 6 Aug 2018 14:39:28 +0000 (16:39 +0200)
html/index.html

index 51a1455d4d5cb3fddd8ee6c8655577004a405fee..d2448522b7a75ca6cb7da85f4589f9ca5b1c0b01 100644 (file)
@@ -3,38 +3,46 @@
 <title>Merica Terminal</title>
 <script type="text/javascript" src="//dejvice.merica.cz:5000/js" async></script>
 <script>
-    var socket = new WebSocket(
-        "ws://" + document.domain + ':' + location.port,
-        "merica-terminal-protocol"
-    );
+    var socket;
 
-    function updateLocal(msg) {
-        document.getElementById("localJSON").innerHTML = msg;
-    }
+    function connect() {
+        console.log("connecting...");
+       socket = new WebSocket(
+            "ws://" + document.domain + ':' + location.port,
+            "merica-terminal-protocol"
+       );
 
-    socket.onopen = function() {
-        console.log("socket open");
-        updateLocal("socket open");
-    }
+       socket.onopen = function() {
+            console.log("socket open");
+            updateLocal("socket open");
+       }
 
-    socket.onclose = function() {
-        console.log("socket closed");
-        updateLocal("socket closed");
-    }
+       socket.onclose = function() {
+            console.log("socket closed");
+            updateLocal("socket closed");
+           delete socket;
+           setTimeout(connect, 10000);
+       }
 
-    socket.onmessage = function(msg) {
-        updateLocal(msg.data);
-        if (typeof updateRemote === "function") {
-            updateRemote(msg.data);
-        } else {
-            if (localStorage) {
-                var now = Date.now();
-                localStorage.setItem(now, msg.data);
-                console.log(now + ": " + msg.data);
+       socket.onmessage = function(msg) {
+            updateLocal(msg.data);
+            if (typeof updateRemote === "function") {
+               updateRemote(msg.data);
+            } else {
+               if (localStorage) {
+                    var now = Date.now();
+                    localStorage.setItem(now, msg.data);
+                    console.log(now + ": " + msg.data);
+               }
             }
-        }
+       }
     }
 
+    connect();
+
+    function updateLocal(msg) {
+        document.getElementById("localJSON").innerHTML = msg;
+    }
     function printLocalStorage() {
         var output = "";
         if (localStorage) {