From abc3ec1630347c66070b44d0f198732773e7df35 Mon Sep 17 00:00:00 2001
From: Nils G <nils.gondermann@ruhr-uni-bochum.de>
Date: Fri, 17 Apr 2020 12:16:44 +0200
Subject: [PATCH] Move websocket API into seperate python module

---
 controllers/game.py       | 12 +++++-------
 modules/websocket_util.py | 10 ++++++++++
 views/game/index.html     |  7 +++++--
 3 files changed, 20 insertions(+), 9 deletions(-)
 create mode 100644 modules/websocket_util.py

diff --git a/controllers/game.py b/controllers/game.py
index 5b67df5..7f7f9e7 100644
--- a/controllers/game.py
+++ b/controllers/game.py
@@ -1,8 +1,7 @@
 from parameter_util import JSON_BODY, JSON_CONTAINS
-from http_util import FAIL, CODE_JSON, CODE_MISSING, CODE_SEMANTIC, CODE_GONE, CODE_CONFLICT, GET_HOST
+from http_util import FAIL, CODE_JSON, CODE_MISSING, CODE_SEMANTIC, CODE_GONE, CODE_CONFLICT
 from room_util import ROOM_GET
-
-from gluon.contrib.websocket_messaging import websocket_send
+from websocket_util import WEBSOCKET_SEND
 
 import json
 
@@ -24,8 +23,8 @@ def start():
     if not room_record:
         return(FAIL(CODE_SEMANTIC))
 
-    if room_record.started:
-        return(FAIL(CODE_CONFLICT))
+    #if room_record.started:
+    #    return(FAIL(CODE_CONFLICT))
 
     if room_record.closed:
         return(FAIL(CODE_GONE))
@@ -34,8 +33,7 @@ def start():
 
     db(db.Room.id == room_record.id).update(started=True, roles=roles)
 
-
-    websocket_send('http://' + GET_HOST() + ':8888', roles, 'mykey', room_record.code)
+    WEBSOCKET_SEND(room_record, "roles", roles)
 
     json_response = {"status": "success"}
     return(response.json(json_response))
diff --git a/modules/websocket_util.py b/modules/websocket_util.py
new file mode 100644
index 0000000..84db71d
--- /dev/null
+++ b/modules/websocket_util.py
@@ -0,0 +1,10 @@
+from gluon import *
+from gluon.contrib.websocket_messaging import websocket_send
+
+from http_util import GET_HOST
+
+def WEBSOCKET_SEND(_room, _type, _data):
+    websocket_send('http://' + GET_HOST() + ':8888', _data, '67c371fd204fbcbf9742310d1a9d951bcca1aa86', _room.code)
+
+def GET_URL(_room):
+    return("ws://"+GET_HOST()+":8888/realtime/"+_room.code)
diff --git a/views/game/index.html b/views/game/index.html
index b60d36b..535a5e5 100644
--- a/views/game/index.html
+++ b/views/game/index.html
@@ -1,5 +1,5 @@
 {{include 'include/popup.html'}}
-{{ from http_util import GET_HOST }}
+{{ from websocket_util import GET_URL }}
 
 <!-- ############################### -->
 <!-- #.............................# -->
@@ -30,12 +30,15 @@
     $(document).ready(function(){
 
         if ('WebSocket' in window) {
-            var ws = new WebSocket("ws://{{=GET_HOST()}}:8888/realtime/{{=room_record.code}}");
+            var ws = new WebSocket("{{=GET_URL(room_record)}}");
             ws.onopen = function () {};
             ws.onmessage = function(e){
                 set_info(e.data);
                 render(name, role, JSON.parse(e.data));
             };
+            ws.onerror = function(e) {
+                set_error("{{=T('Error')}}: "+e);
+            };
             ws.onclose =function () {
                 set_error("{{=T('WebSocket has closed')}}. {{=T('Please refresh this website')}}.");
             };
-- 
GitLab