move live view to websockets

This commit is contained in:
Tim Bentley 2016-06-04 20:32:50 +01:00
parent ea382e18bb
commit 251f197e45
4 changed files with 33 additions and 10 deletions

View File

@ -28,18 +28,23 @@ window.OpenLP = {
);
},
pollServer: function () {
$.getJSON(
"/main/poll",
function (data, status) {
if (OpenLP.slideCount != data.results.slide_count) {
OpenLP.slideCount = data.results.slide_count;
OpenLP.loadSlide();
if ("WebSocket" in window) {
// Let us open a web socket
var ws = new WebSocket('ws://' + location.hostname + ':4318/main_poll');
ws.binaryType = 'arraybuffer';
ws.onmessage = function (evt) {
var msg = JSON.parse(String.fromCharCode.apply(null, new Uint8Array(evt.data)));
if (OpenLP.slideCount != msg.results.slide_count) {
OpenLP.slideCount = msg.results.slide_count;
OpenLP.loadSlide();
}
}
}
);
} else {
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
}
};
$.ajaxSetup({ cache: false });
setInterval("OpenLP.pollServer();", 500);
OpenLP.pollServer();

View File

@ -198,6 +198,7 @@ class OpenLPServer(RegistryProperties, OpenLPMixin):
"""
log.debug("web socket handler registered with client")
previous_poll = None
previous_main_poll = None
if path == '/poll':
while True:
current_poll = OpenLPPoll().poll()
@ -205,6 +206,13 @@ class OpenLPServer(RegistryProperties, OpenLPMixin):
await request.send(current_poll)
previous_poll = current_poll
await asyncio.sleep(0.2)
elif path == '/main_poll':
while True:
main_poll = OpenLPPoll().main_poll()
if main_poll != previous_main_poll:
await request.send(main_poll)
previous_main_poll = main_poll
await asyncio.sleep(0.2)
def stop_server(self):
"""

View File

@ -50,3 +50,12 @@ class OpenLPPoll(RegistryProperties):
'isAuthorised': False
}
return json.dumps({'results': result}).encode()
def main_poll(self):
"""
Poll OpenLP to determine the current slide count.
"""
result = {
'slide_count': self.live_controller.slide_count
}
return json.dumps({'results': result}).encode()

View File

@ -94,6 +94,7 @@ MODULES = [
'mako',
'uno',
'websockets',
'asyncio',
'six'
]