-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restart keepalive interval with reduced delay after user input
- Loading branch information
hhaensel
committed
Nov 11, 2024
1 parent
b75ca84
commit d2072bf
Showing
2 changed files
with
15 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters