move main to websockets

This commit is contained in:
Tim Bentley 2016-06-04 22:00:56 +01:00
parent 251f197e45
commit 055280860c
1 changed files with 19 additions and 13 deletions

View File

@ -159,15 +159,19 @@ window.OpenLP = {
); );
}, },
pollServer: function () { pollServer: function () {
$.getJSON( if ("WebSocket" in window) {
"/api/poll", // Let us open a web socket
function (data, status) { var ws = new WebSocket('ws://' + location.hostname + ':4318/poll');
var prevItem = OpenLP.currentItem; ws.binaryType = 'arraybuffer';
OpenLP.currentSlide = data.results.slide; ws.onmessage = function (evt) {
OpenLP.currentItem = data.results.item; var data = JSON.parse(String.fromCharCode.apply(null, new Uint8Array(evt.data)));
OpenLP.isSecure = data.results.isSecure;
OpenLP.isAuthorised = data.results.isAuthorised; var prevItem = OpenLP.currentItem;
if ($("#service-manager").is(":visible")) { OpenLP.currentSlide = data.results.slide;
OpenLP.currentItem = data.results.item;
OpenLP.isSecure = data.results.isSecure;
OpenLP.isAuthorised = data.results.isAuthorised;
if ($("#service-manager").is(":visible")) {
if (OpenLP.currentService != data.results.service) { if (OpenLP.currentService != data.results.service) {
OpenLP.currentService = data.results.service; OpenLP.currentService = data.results.service;
OpenLP.loadService(); OpenLP.loadService();
@ -185,7 +189,7 @@ window.OpenLP = {
}); });
$("#service-manager div[data-role=content] ul[data-role=listview]").listview("refresh"); $("#service-manager div[data-role=content] ul[data-role=listview]").listview("refresh");
} }
if ($("#slide-controller").is(":visible")) { if ($("#slide-controller").is(":visible")) {
if (prevItem != OpenLP.currentItem) { if (prevItem != OpenLP.currentItem) {
OpenLP.loadController(); OpenLP.loadController();
return; return;
@ -205,8 +209,11 @@ window.OpenLP = {
}); });
$("#slide-controller div[data-role=content] ul[data-role=listview]").listview("refresh"); $("#slide-controller div[data-role=content] ul[data-role=listview]").listview("refresh");
} }
} }
); } else {
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}, },
nextItem: function (event) { nextItem: function (event) {
event.preventDefault(); event.preventDefault();
@ -380,5 +387,4 @@ $.ajaxSetup({cache: false});
$("#search").live("pageinit", function (event) { $("#search").live("pageinit", function (event) {
OpenLP.getSearchablePlugins(); OpenLP.getSearchablePlugins();
}); });
setInterval("OpenLP.pollServer();", 500);
OpenLP.pollServer(); OpenLP.pollServer();