Skip to content

Instantly share code, notes, and snippets.

@istudyatuni
Created October 23, 2020 17:45
Show Gist options
  • Save istudyatuni/4940d5e4ce437b54f94bcf2aab9704ef to your computer and use it in GitHub Desktop.
Save istudyatuni/4940d5e4ce437b54f94bcf2aab9704ef to your computer and use it in GitHub Desktop.
let timerID = 0
let ws = new WebSocket(url)
ws.onopen = () => {
// some code
keepAlive()
}
ws.onclose = () => {
// some code
cancelKeepAlive()
}
function keepAlive() {
var timeout = 20000;
if (ws.readyState == ws.OPEN) {
ws.send('');
}
timerId = setTimeout(keepAlive, timeout);
}
function cancelKeepAlive() {
if (timerId) {
clearTimeout(timerId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment