Created
October 23, 2020 17:45
-
-
Save istudyatuni/4940d5e4ce437b54f94bcf2aab9704ef to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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