From b63c5f27853aded30c492bdb94a680e5a9c9648b Mon Sep 17 00:00:00 2001 From: Mario Date: Sat, 14 Nov 2020 21:28:50 +0000 Subject: Polling fallback to server sent events. Polling is the default. SSE must be enabled in /admin/site > Advanced > Enable SSE Notifications if desired. --- view/js/main.js | 82 ++++++++++++++++++++++++++++++++++--------------- view/tpl/admin_site.tpl | 1 + view/tpl/head.tpl | 1 + 3 files changed, 59 insertions(+), 25 deletions(-) (limited to 'view') diff --git a/view/js/main.js b/view/js/main.js index 8ceb0b143..560868046 100644 --- a/view/js/main.js +++ b/view/js/main.js @@ -31,6 +31,7 @@ var sse_offset = 0; var sse_type; var sse_partial_result = false; var sse_rmids = []; +var sse_fallback_interval; var page_cache = {}; @@ -94,37 +95,56 @@ $(document).ready(function() { jQuery.timeago.settings.allowFuture = true; - if(typeof(window.SharedWorker) === 'undefined') { - // notifications with multiple tabs open will not work very well in this scenario - var evtSource = new EventSource('/sse'); - evtSource.addEventListener('notifications', function(e) { - var obj = JSON.parse(e.data); - sse_handleNotifications(obj, false, false); - }, false); + if(sse_enabled) { + if(typeof(window.SharedWorker) === 'undefined') { + // notifications with multiple tabs open will not work very well in this scenario + var evtSource = new EventSource('/sse'); - document.addEventListener('visibilitychange', function() { - if (!document.hidden) { - sse_offset = 0; - sse_bs_init(); - } - }, false); + evtSource.addEventListener('notifications', function(e) { + var obj = JSON.parse(e.data); + sse_handleNotifications(obj, false, false); + }, false); - } - else { - var myWorker = new SharedWorker('/view/js/sse_worker.js', localUser); + document.addEventListener('visibilitychange', function() { + if (!document.hidden) { + sse_offset = 0; + sse_bs_init(); + } + }, false); - myWorker.port.onmessage = function(e) { - obj = e.data; - console.log(obj); - sse_handleNotifications(obj, false, false); } + else { + var myWorker = new SharedWorker('/view/js/sse_worker.js', localUser); - myWorker.onerror = function(e) { - myWorker.port.close(); + myWorker.port.onmessage = function(e) { + obj = e.data; + console.log(obj); + sse_handleNotifications(obj, false, false); + } + + myWorker.onerror = function(e) { + myWorker.port.close(); + } + + myWorker.port.start(); } + } + else { + if (!document.hidden) + sse_fallback_interval = setInterval(sse_fallback, updateInterval); - myWorker.port.start(); + document.addEventListener('visibilitychange', function() { + if (document.hidden) { + clearInterval(sse_fallback_interval); + } + else { + sse_offset = 0; + sse_bs_init(); + sse_fallback_interval = setInterval(sse_fallback, updateInterval); + } + + }, false); } $('.notification-link').on('click', { replace: true, followup: false }, sse_bs_notifications); @@ -224,6 +244,8 @@ $(document).ready(function() { cache_next_page(); }); + + }); function getConversationSettings() { @@ -1763,8 +1785,6 @@ function sse_bs_init() { } function sse_bs_counts() { - - if(sse_bs_active) return; @@ -2022,3 +2042,15 @@ function sse_setNotificationsStatus() { } } + +function sse_fallback() { + $.get('/sse', function(obj) { + if(! obj) + return; + + console.log('sse fallback'); + console.log(obj); + + sse_handleNotifications(obj, false, false); + }); +} diff --git a/view/tpl/admin_site.tpl b/view/tpl/admin_site.tpl index 5e10e6eea..8d32ba9a4 100755 --- a/view/tpl/admin_site.tpl +++ b/view/tpl/admin_site.tpl @@ -95,6 +95,7 @@

{{$advanced}}

+ {{include file="field_checkbox.tpl" field=$sse_enabled}} {{include file="field_input.tpl" field=$imagick_path}} {{include file="field_input.tpl" field=$proxy}} {{include file="field_input.tpl" field=$proxyuser}} diff --git a/view/tpl/head.tpl b/view/tpl/head.tpl index bd4cf3747..0d212e029 100755 --- a/view/tpl/head.tpl +++ b/view/tpl/head.tpl @@ -9,6 +9,7 @@ {{$plugins}}