diff --git a/Assistant/Threads/WebApp.hs b/Assistant/Threads/WebApp.hs index e75870e0d7..7b96665d32 100644 --- a/Assistant/Threads/WebApp.hs +++ b/Assistant/Threads/WebApp.hs @@ -146,6 +146,7 @@ autoUpdate :: Text -> Route WebApp -> Int -> Int -> Widget autoUpdate ident gethtml ms_delay ms_startdelay = do let delay = show ms_delay let startdelay = show ms_startdelay + addScript $ StaticR longpolling_js $(widgetFile "longpolling") {- A display of currently running and queued transfers. diff --git a/static/longpolling.js b/static/longpolling.js new file mode 100644 index 0000000000..4e5f102f00 --- /dev/null +++ b/static/longpolling.js @@ -0,0 +1,41 @@ +// Uses long-polling to update a div with a specified id, +// by polling an url, which should return a new div, with the same id. + +connfails=0; + +connfailed= + '' ; + +function longpoll(url, divid) { + (function( $ ) { + $.ajax({ + 'url': url, + 'dataType': 'html', + 'success': function(data, status, jqxhr) { + $('#' + divid).replaceWith(data); + connfails=0; + return 1; + }, + 'error': function(jqxhr, msg, e) { + connfails=connfails+1; + if (connfails > 3) { + // blocked by many browsers + window.close(); + $('#modal').replaceWith(connfailed); + $('#modal').modal('show'); + return 0; + } + else { + return 1; + } + } + }); + })( jQuery ); +} diff --git a/templates/longpolling.julius b/templates/longpolling.julius index 35205bd04d..29e533c418 100644 --- a/templates/longpolling.julius +++ b/templates/longpolling.julius @@ -1,54 +1,11 @@ - -// Uses long-polling to update a div with id=#{ident} -// The gethtml route should return a new div, with the same id. -// -// Maximum update frequency is controlled by #{startdelay} -// and #{delay}, both in milliseconds. - -connfails=0; - -connfailed= - '' ; - -(function( $ ) { - -$.LongPoll#{ident} = (function() { - return { - send : function() { - $.ajax({ - 'url': '@{gethtml}', - 'dataType': 'html', - 'success': function(data, status, jqxhr) { - $('##{ident}').replaceWith(data); - setTimeout($.LongPoll#{ident}.send, #{show delay}); - numerrs=0; - }, - 'error': function(jqxhr, msg, e) { - connfails=connfails+1; - if (connfails > 3) { - // blocked by many browsers - window.close(); - $('#modal').replaceWith(connfailed); - $('#modal').modal('show'); - } - else { - setTimeout($.LongPoll#{ident}.send, #{show delay}); - } - }, - }); - } +// longpolling for #{ident} +function poller#{ident}() { + if (longpoll('@{gethtml}', '#{ident}')) { + setTimeout(poller#{ident}, #{delay}); } -}()); - -$(document).bind('ready.app', function() { - setTimeout($.LongPoll#{ident}.send, #{show startdelay}); -}); - +} +(function( $ ) { + $(document).bind('ready.app', function() { + setTimeout(poller#{ident}, #{startdelay}); + }); })( jQuery );