From d2072bfcfc92690cc8807a133d389243deece849 Mon Sep 17 00:00:00 2001 From: hhaensel Date: Mon, 11 Nov 2024 08:15:17 +0100 Subject: [PATCH] restart keepalive interval with reduced delay after user input --- assets/js/keepalive.js | 18 ++++++++++++++---- src/Elements.jl | 3 +-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/assets/js/keepalive.js b/assets/js/keepalive.js index 01a5d36..e577401 100644 --- a/assets/js/keepalive.js +++ b/assets/js/keepalive.js @@ -1,22 +1,32 @@ /* -** keepalive.js // v1.0.0 // 6th January 2022 +** keepalive.js // v1.1.0 // 11 November 2024 ** Keeps alive the websocket connection by sending a ping every x seconds ** where x = Genie.config.webchannels_keepalive_frequency */ function keepalive(WebChannel) { if (WebChannel.lastMessageAt !== undefined) { - if (Date.now() - WebChannel.lastMessageAt + 200 < Genie.Settings.webchannels_keepalive_frequency) { - return + dt = Date.now() - WebChannel.lastMessageAt; + // allow for a 200ms buffer + if (dt + 200 < Genie.Settings.webchannels_keepalive_frequency) { + keepaliveTimer(WebChannel, Genie.Settings.webchannels_keepalive_frequency - dt); + return; } } if (Genie.Settings.env == 'dev') { console.info('Keeping connection alive'); - console.log(WebChannel.parent.i) } WebChannel.sendMessageTo(WebChannel.channel, 'keepalive', { 'payload': {} }); } + +function keepaliveTimer(WebChannel, startDelay = Genie.Settings.webchannels_keepalive_frequency) { + clearInterval(WebChannel.keepalive_interval); + setTimeout(() => { + keepalive(WebChannel); + WebChannel.keepalive_interval = setInterval(() => keepalive(WebChannel), Genie.Settings.webchannels_keepalive_frequency); + }, startDelay) +} \ No newline at end of file diff --git a/src/Elements.jl b/src/Elements.jl index 2301df9..200ffee 100644 --- a/src/Elements.jl +++ b/src/Elements.jl @@ -222,8 +222,7 @@ function vue_integration(::Type{M}; " try { if (Genie.Settings.webchannels_keepalive_frequency > 0) { - clearInterval(app.WebChannel.keepalive_interval); - app.WebChannel.keepalive_interval = setInterval(() => keepalive(app.WebChannel), Genie.Settings.webchannels_keepalive_frequency); + keepaliveTimer(app.WebChannel, 0); } } catch (e) { if (Genie.Settings.env === 'dev') {