Ping more consistent

This commit is contained in:
bingus_violet 2024-04-20 17:00:58 -05:00
parent 17327706f5
commit e9e3f05208

View file

@ -66,6 +66,19 @@ window.onload = function () {
var lastPong = Date.now() var lastPong = Date.now()
function ping(dur) {
sock.send(JSON.stringify({
op: 3
}))
setTimeout(() => {
ping(dur)
if (Date.now() - lastPong > 120000) {
sock.close()
console.log("Max duration since last pong exceeded- Closing socket.")
}
}, dur);
}
function socketeer() { function socketeer() {
sock = new WebSocket('wss://api.violets-purgatory.dev') sock = new WebSocket('wss://api.violets-purgatory.dev')
@ -84,20 +97,6 @@ function socketeer() {
}, 30000); }, 30000);
}) })
function ping(dur) {
sock.send(JSON.stringify({
op: 3
}))
setTimeout(() => {
ping(dur)
if (Date.now() - lastPong > 120000) {
sock.close()
console.log("Max duration since last pong exceeded- Closing socket.")
}
}, dur);
}
sock.addEventListener("message", async(data) => { sock.addEventListener("message", async(data) => {
data = data.data data = data.data
data = JSON.parse(data) data = JSON.parse(data)
@ -107,6 +106,8 @@ function socketeer() {
} else if (data.op == 0) { } else if (data.op == 0) {
var discFetch = await (await fetch("/discHTML")).text() var discFetch = await (await fetch("/discHTML")).text()
document.querySelector("#activityHTML").innerHTML = discFetch document.querySelector("#activityHTML").innerHTML = discFetch
}else if (data.op == 3) {
lastPong = Date.now()
} else { } else {
console.log(data) console.log(data)
} }