From 1efd9dc2a75546c38db1e1452a6a231ef7f9ca36 Mon Sep 17 00:00:00 2001 From: bingus_violet Date: Mon, 18 Mar 2024 20:57:48 -0500 Subject: [PATCH] Less crashing, websocket more consistent --- index.js | 10 +++++++++- pageUpdater.js | 19 +++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 8feff54..b0efb37 100644 --- a/index.js +++ b/index.js @@ -31,4 +31,12 @@ if (!fs.existsSync(cachePath)) { } } -app.use(pageUpdater.middleWare) \ No newline at end of file +app.use(pageUpdater.middleWare) + +process.on('uncaughtException', (err, origin) => { + fs.writeSync( + process.stderr.fd, + `Caught exception: ${err}\n` + + `Exception origin: ${origin}`, + ); + }); \ No newline at end of file diff --git a/pageUpdater.js b/pageUpdater.js index 3cb0018..64abe53 100644 --- a/pageUpdater.js +++ b/pageUpdater.js @@ -156,7 +156,7 @@ updateCommits() // Lanyard Stuffs var lastLanyardUpdate = Date.now() -var lastPong = Date.now() +var lastPong = 0 var activityImages = config.activityImages var cachedImages = {} @@ -188,6 +188,18 @@ function get_img_url(activity, size = "large_image") { function socketeer() { 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) { lanyard.send(JSON.stringify({ op: 3 @@ -196,7 +208,7 @@ function socketeer() { ping(dur) if (Date.now() - lastPong > 120000) { lanyard.close() - socketeer() + console.log("Max duration since last pong exceeded- Closing socket.") } }, dur); } @@ -204,10 +216,9 @@ function socketeer() { lanyard.addEventListener("message", async (res) => { var data = JSON.parse(res.data) if (data.op == 1) { + console.log("Connected to Discord Websocket!") ping(30000) lastPong = Date.now() - } else if (data.op == 3) { - lastPong = Date.now() } else if (data.op == 0) { lanyardData = data.d lastLanyardUpdate = Date.now()