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

Move websocket API into seperate python module

parent 759a39fb
No related branches found
No related tags found
No related merge requests found
from parameter_util import JSON_BODY, JSON_CONTAINS 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 room_util import ROOM_GET
from websocket_util import WEBSOCKET_SEND
from gluon.contrib.websocket_messaging import websocket_send
import json import json
...@@ -24,8 +23,8 @@ def start(): ...@@ -24,8 +23,8 @@ def start():
if not room_record: if not room_record:
return(FAIL(CODE_SEMANTIC)) return(FAIL(CODE_SEMANTIC))
if room_record.started: #if room_record.started:
return(FAIL(CODE_CONFLICT)) # return(FAIL(CODE_CONFLICT))
if room_record.closed: if room_record.closed:
return(FAIL(CODE_GONE)) return(FAIL(CODE_GONE))
...@@ -34,8 +33,7 @@ def start(): ...@@ -34,8 +33,7 @@ def start():
db(db.Room.id == room_record.id).update(started=True, roles=roles) db(db.Room.id == room_record.id).update(started=True, roles=roles)
WEBSOCKET_SEND(room_record, "roles", roles)
websocket_send('http://' + GET_HOST() + ':8888', roles, 'mykey', room_record.code)
json_response = {"status": "success"} json_response = {"status": "success"}
return(response.json(json_response)) return(response.json(json_response))
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)
{{include 'include/popup.html'}} {{include 'include/popup.html'}}
{{ from http_util import GET_HOST }} {{ from websocket_util import GET_URL }}
<!-- ############################### --> <!-- ############################### -->
<!-- #.............................# --> <!-- #.............................# -->
...@@ -30,12 +30,15 @@ ...@@ -30,12 +30,15 @@
$(document).ready(function(){ $(document).ready(function(){
if ('WebSocket' in window) { 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.onopen = function () {};
ws.onmessage = function(e){ ws.onmessage = function(e){
set_info(e.data); set_info(e.data);
render(name, role, JSON.parse(e.data)); render(name, role, JSON.parse(e.data));
}; };
ws.onerror = function(e) {
set_error("{{=T('Error')}}: "+e);
};
ws.onclose =function () { ws.onclose =function () {
set_error("{{=T('WebSocket has closed')}}. {{=T('Please refresh this website')}}."); set_error("{{=T('WebSocket has closed')}}. {{=T('Please refresh this website')}}.");
}; };
......
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