Skip to content

Commit

Permalink
restart keepalive interval with reduced delay after user input
Browse files Browse the repository at this point in the history
  • Loading branch information
hhaensel committed Nov 11, 2024
1 parent b75ca84 commit d2072bf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
18 changes: 14 additions & 4 deletions assets/js/keepalive.js
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 1 addition & 2 deletions src/Elements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down

0 comments on commit d2072bf

Please sign in to comment.