Skip to content
Snippets Groups Projects
Commit 759a39fb authored by Nils G.'s avatar Nils G.
Browse files

Add websockets to game to retrieve changes in the room without refresh or ajax

parent f0cd6486
No related branches found
No related tags found
No related merge requests found
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))
......@@ -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)
......@@ -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;
}
......
{{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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment