Violets-Purgatory/static/js/lanyardSocket.js

79 lines
2.3 KiB
JavaScript
Raw Permalink Normal View History

var sock
var firsttimeDebounce = true
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() {
sock = new WebSocket('wss://api.violets-purgatory.dev')
sock.addEventListener("open", () => {
ping(30000)
})
sock.addEventListener("error", (error) => {
console.log(error)
})
sock.addEventListener("close", () => {
console.log("Connection Closed. Attempting Reconnect in 30 seconds.")
$("#apiConnected").text("false")
setTimeout(() => {
socketeer()
}, 30000);
})
sock.addEventListener("message", async (data) => {
data = data.data
data = JSON.parse(data)
if (data.op == 4) {
$("#apiConnected").text("true")
globalSpins = data.spins
if (firsttimeDebounce == true) {
firsttimeDebounce = false
$(".globalSpins").innerHTML = globalSpins + 1;
} else {
$(".globalSpins").innerHTML = globalSpins;
}
} else if (data.op == 0) {
var lanyard = data.d
var statusInfo = discStatuses[lanyard.discord_status]
var lastStatus = $(".statusColor")
if (lastStatus.text() != statusInfo.text) {
lastStatus.text(statusInfo.text)
lastStatus.css("color", statusInfo.color)
$(".pfp").css("borderColor", statusInfo.color)
}
// if (lanyard.activities[0] && lanyard.activities[0].type == 4) {
// document.querySelector(".customStatus").innerHTML = `<hr><img src=""><p>${lanyard.activities[0].state}</p>`
// } else {
// document.querySelector(".customStatus").innerHTML = ""
// }
var discFetch = await (await fetch("/discHTML")).text()
$("#activityHtml").html(discFetch)
} else if (data.op == 3) {
lastPong = Date.now()
} else {
console.log(data)
}
})
}
socketeer()