diff --git a/controllers/game.py b/controllers/game.py index 3ca1e3372a703ca782d935553fdd1c1cdcda282a..5b67df54d2f876e29e2e7b8b3c04dfdcb2331b8e 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 dd90ad4c99d381132432ba5e92d859e9287e6de3..1d3d71d510a9c37563438e2fd7d56bb99ebd8ffc 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 984593c93526ff4bef63cb337b800915fbc75bc8..45c3c147ceab1f2b895e806ba547799cd4ede91f 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 fbc0539d653b4a9c3e756850883a15e5c4694903..b60d36b3a73b2b8d134c34931c402e9f4102dec2 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>