diff --git a/controllers/game.py b/controllers/game.py index 5b67df54d2f876e29e2e7b8b3c04dfdcb2331b8e..7f7f9e7e926163451c5dad1322c9e62195d46e08 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 0000000000000000000000000000000000000000..84db71d9cb417621cdf67e8fbbf612a7f7df6e43 --- /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 b60d36b3a73b2b8d134c34931c402e9f4102dec2..535a5e53e19ac3ab66fd3aecd361b555171d16b8 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')}}."); };