Less crashing, websocket more consistent

This commit is contained in:
bingus_violet 2024-03-18 20:57:48 -05:00
parent 49317e4795
commit 1efd9dc2a7
2 changed files with 24 additions and 5 deletions

View file

@ -31,4 +31,12 @@ if (!fs.existsSync(cachePath)) {
} }
} }
app.use(pageUpdater.middleWare) app.use(pageUpdater.middleWare)
process.on('uncaughtException', (err, origin) => {
fs.writeSync(
process.stderr.fd,
`Caught exception: ${err}\n` +
`Exception origin: ${origin}`,
);
});

View file

@ -156,7 +156,7 @@ updateCommits()
// Lanyard Stuffs // Lanyard Stuffs
var lastLanyardUpdate = Date.now() var lastLanyardUpdate = Date.now()
var lastPong = Date.now() var lastPong = 0
var activityImages = config.activityImages var activityImages = config.activityImages
var cachedImages = {} var cachedImages = {}
@ -188,6 +188,18 @@ function get_img_url(activity, size = "large_image") {
function socketeer() { function socketeer() {
var lanyard = new WebSocket('https://api.violets-purgatory.dev') var lanyard = new WebSocket('https://api.violets-purgatory.dev')
lanyard.on("error", (error) =>{
console.log(error)
})
lanyard.on("close", () => {
console.log("Connection Closed. Attempting Reconnect in 30 seconds.")
setTimeout(() => {
socketeer()
}, 3000);
})
function ping(dur) { function ping(dur) {
lanyard.send(JSON.stringify({ lanyard.send(JSON.stringify({
op: 3 op: 3
@ -196,7 +208,7 @@ function socketeer() {
ping(dur) ping(dur)
if (Date.now() - lastPong > 120000) { if (Date.now() - lastPong > 120000) {
lanyard.close() lanyard.close()
socketeer() console.log("Max duration since last pong exceeded- Closing socket.")
} }
}, dur); }, dur);
} }
@ -204,10 +216,9 @@ function socketeer() {
lanyard.addEventListener("message", async (res) => { lanyard.addEventListener("message", async (res) => {
var data = JSON.parse(res.data) var data = JSON.parse(res.data)
if (data.op == 1) { if (data.op == 1) {
console.log("Connected to Discord Websocket!")
ping(30000) ping(30000)
lastPong = Date.now() lastPong = Date.now()
} else if (data.op == 3) {
lastPong = Date.now()
} else if (data.op == 0) { } else if (data.op == 0) {
lanyardData = data.d lanyardData = data.d
lastLanyardUpdate = Date.now() lastLanyardUpdate = Date.now()