From 759a39fb3fe79da69226304c7a43d7fa52e49abd Mon Sep 17 00:00:00 2001
From: Nils G <nils.gondermann@ruhr-uni-bochum.de>
Date: Thu, 16 Apr 2020 14:03:08 +0200
Subject: [PATCH] Add websockets to game to retrieve changes in the room
 without refresh or ajax

---
 controllers/game.py      | 11 +++++++++--
 modules/http_util.py     |  7 +++++++
 static/renderer/debug.js |  2 +-
 views/game/index.html    | 25 ++++++++++++++++++++++++-
 4 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/controllers/game.py b/controllers/game.py
index 3ca1e33..5b67df5 100644
--- a/controllers/game.py
+++ b/controllers/game.py
@@ -1,7 +1,9 @@
 from parameter_util import JSON_BODY, JSON_CONTAINS
-from http_util import FAIL, CODE_JSON, CODE_MISSING, CODE_SEMANTIC, CODE_GONE, CODE_CONFLICT
+from http_util import FAIL, CODE_JSON, CODE_MISSING, CODE_SEMANTIC, CODE_GONE, CODE_CONFLICT, GET_HOST
 from room_util import ROOM_GET
 
+from gluon.contrib.websocket_messaging import websocket_send
+
 import json
 
 def start():
@@ -28,7 +30,12 @@ def start():
     if room_record.closed:
         return(FAIL(CODE_GONE))
 
-    db(db.Room.id == room_record.id).update(started=True, roles=json.dumps(roles))
+    roles = json.dumps(roles)
+
+    db(db.Room.id == room_record.id).update(started=True, roles=roles)
+
+
+    websocket_send('http://' + GET_HOST() + ':8888', roles, 'mykey', room_record.code)
 
     json_response = {"status": "success"}
     return(response.json(json_response))
diff --git a/modules/http_util.py b/modules/http_util.py
index dd90ad4..1d3d71d 100644
--- a/modules/http_util.py
+++ b/modules/http_util.py
@@ -14,3 +14,10 @@ def FAIL(_code):
 def SUCCESS():
     current.response.status = 200
     return(200)
+
+def GET_HOST():
+    http_host = current.request.env.http_host
+    if ":" in http_host:
+        http_host = http_host[:http_host.find(":")]
+
+    return(http_host)
diff --git a/static/renderer/debug.js b/static/renderer/debug.js
index 984593c..45c3c14 100644
--- a/static/renderer/debug.js
+++ b/static/renderer/debug.js
@@ -2,7 +2,7 @@ function render(_name, _role, _roles){
 
     if(_role in _roles) {
         //TODO: Render role according to definition
-        $("#game_content").html(_role);
+        $("#game_content").html(JSON.stringify(_roles));
         return;
     }
 
diff --git a/views/game/index.html b/views/game/index.html
index fbc0539..b60d36b 100644
--- a/views/game/index.html
+++ b/views/game/index.html
@@ -1,4 +1,5 @@
 {{include 'include/popup.html'}}
+{{ from http_util import GET_HOST }}
 
 <!-- ############################### -->
 <!-- #.............................# -->
@@ -21,6 +22,28 @@
 
 <script type="text/javascript">
     $(document).ready(function(){
-        render(name, role, {});
+        render(name, role, JSON.parse('{{=XML(room_record.roles)}}'));
     });
+
+
+    //Websocket connection
+    $(document).ready(function(){
+
+        if ('WebSocket' in window) {
+            var ws = new WebSocket("ws://{{=GET_HOST()}}:8888/realtime/{{=room_record.code}}");
+            ws.onopen = function () {};
+            ws.onmessage = function(e){
+                set_info(e.data);
+                render(name, role, JSON.parse(e.data));
+            };
+            ws.onclose =function () {
+                set_error("{{=T('WebSocket has closed')}}. {{=T('Please refresh this website')}}.");
+            };
+        } else {
+            /* not supported */
+            set_error("{{=T('WebSocket is not supported by your browser')}}.");
+        }
+
+    });
+
 </script>
-- 
GitLab