diff --git a/index.js b/index.js index b0efb37..11ead16 100644 --- a/index.js +++ b/index.js @@ -31,6 +31,10 @@ if (!fs.existsSync(cachePath)) { } } +app.get("/discHTML", (req, res) => { + res.send(pageUpdater.getActivities()) +}) + app.use(pageUpdater.middleWare) process.on('uncaughtException', (err, origin) => { diff --git a/static/index.html b/static/index.html index c3531c8..47c2824 100644 --- a/static/index.html +++ b/static/index.html @@ -55,7 +55,9 @@

Make sure to check out this project on Codeberg!

- {ACTIVITIES} +
+ {ACTIVITIES} +


Services

List of services for public use hosted on Violet's Purgatory.

diff --git a/static/main.js b/static/main.js index 01b24f5..980c921 100644 --- a/static/main.js +++ b/static/main.js @@ -1,7 +1,53 @@ -window.onbeforeunload = function() { - window.scrollTo(0, 0) +window.onbeforeunload = function () { + window.scrollTo(0, 0); } -window.onload = function() { - window.scrollTo(0, 0) -} \ No newline at end of file +window.onload = function () { + window.scrollTo(0, 0); +} + +function socketeer() { + var lanyard = new WebSocket('wss://api.violets-purgatory.dev') + + lanyard.onerror = (error) => { + console.log(error) + } + + lanyard.onclose = () => { + console.log("Connection Closed. Attempting Reconnect in 30 seconds.") + setTimeout(() => { + socketeer() + }, 3000); + } + + function ping(dur) { + lanyard.send(JSON.stringify({ + op: 3 + })) + setTimeout(() => { + ping(dur) + if (Date.now() - lastPong > 120000) { + lanyard.close() + console.log("Max duration since last pong exceeded- Closing socket.") + } + }, dur); + } + + 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() + } + + var discStatusHTML = await (await fetch("/discHTML")).text(); + + var activityDiv = document.querySelector("#activityHTML") + activityDiv.innerHTML = discStatusHTML + }) +} + +socketeer() \ No newline at end of file