From 4c95e8da1225dd0b410ef6b88099f5b445b12504 Mon Sep 17 00:00:00 2001
From: Nils G <nils.gondermann@ruhr-uni-bochum.de>
Date: Sat, 18 Apr 2020 13:36:52 +0200
Subject: [PATCH] Improved websocket error handling

---
 views/game/index.html | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/views/game/index.html b/views/game/index.html
index cb2bf40..45e22c6 100644
--- a/views/game/index.html
+++ b/views/game/index.html
@@ -31,7 +31,9 @@
 
         if ('WebSocket' in window) {
             var ws = new WebSocket("{{=GET_URL(room_record)}}");
-            ws.onopen = function () {};
+            ws.onopen = function () {
+                ws.initialConnect = true;
+            };
             ws.onmessage = function(e) {
                 set_info(e.data);
                 render(name, role, JSON.parse(e.data));
@@ -40,7 +42,13 @@
                 set_error("{{=T('Error')}}: "+e);
             };
             ws.onclose = function () {
-                set_error("{{=T('WebSocket has closed')}}. {{=T('Please refresh this website')}}.");
+                if(ws.initialConnect) {
+                    window.setTimeout(function() {
+                        set_error("{{=T('WebSocket has closed')}}. {{=T('Please refresh this website')}}.");
+                    }, 1000);
+                } else {
+                    set_error("{{=T('WebSocket server is not responding')}}.");
+                }
             };
         } else {
             /* not supported */
-- 
GitLab