Blog in development
This commit is contained in:
parent
2b724bc2db
commit
7e52727bf0
12 changed files with 389 additions and 10 deletions
55
api.js
55
api.js
|
@ -14,6 +14,9 @@ module.exports = {
|
|||
"connected": false,
|
||||
"lastLanyardUpdate": Date.now(),
|
||||
|
||||
"blogConnected": false,
|
||||
"blogPosts": undefined,
|
||||
|
||||
"events": events,
|
||||
|
||||
"spins": 0,
|
||||
|
@ -69,4 +72,54 @@ function socketeer() {
|
|||
})
|
||||
}
|
||||
|
||||
socketeer()
|
||||
socketeer()
|
||||
|
||||
function blogSocket() {
|
||||
var blog = new WebSocket('https://blog.violets-purgatory.dev')
|
||||
|
||||
blog.on("error", (error) => {
|
||||
console.log(error)
|
||||
})
|
||||
|
||||
blog.on("close", () => {
|
||||
console.log("Connection Closed. Attempting Reconnect in 30 seconds.")
|
||||
module.exports.blogConnected = false
|
||||
setTimeout(() => {
|
||||
blogSocket()
|
||||
}, 30000);
|
||||
})
|
||||
|
||||
function ping(dur) {
|
||||
blog.send(JSON.stringify({
|
||||
type: "ping"
|
||||
}))
|
||||
setTimeout(() => {
|
||||
ping(dur)
|
||||
if (Date.now() - lastPong > 120000) {
|
||||
blog.close()
|
||||
console.log("Max duration since last pong exceeded- Closing socket.")
|
||||
}
|
||||
}, dur);
|
||||
}
|
||||
|
||||
blog.addEventListener("message", async (res) => {
|
||||
var data = JSON.parse(res.data)
|
||||
if (data.type == "init") {
|
||||
console.log("Connected to Blog Websocket!")
|
||||
module.exports.blogConnected = true
|
||||
ping(30000)
|
||||
lastPong = Date.now()
|
||||
events.emit("blogConnect")
|
||||
} else if (data.type == "ping") {
|
||||
lastPong = Date.now()
|
||||
} else if (data.type == "allPosts") {
|
||||
console.log("Recieved posts!")
|
||||
module.exports.blogPosts = data.data
|
||||
events.emit("blogUpdate")
|
||||
} else {
|
||||
console.log(data)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
blogSocket()
|
Loading…
Add table
Add a link
Reference in a new issue