Reconnect automatically

This commit is contained in:
Bingus_Violet 2024-02-19 10:57:50 -06:00
parent b11d5b51ed
commit 26b2bdc6e5

View file

@ -34,47 +34,53 @@ app.get("/v1/lanyard", (req, res) => {
// console.log("API is now listening on port 8080!")
// })
var lanyard = new WebSocket('wss://api.lanyard.rest/socket')
function socketeer() {
var lanyard = new WebSocket('wss://api.lanyard.rest/socket')
function beat(dur) {
lanyard.send(JSON.stringify({
op: 3
}))
setTimeout(() => {
beat(dur)
}, dur);
}
lanyard.addEventListener("message", async (res) => {
var data = JSON.parse(res.data)
if (data.op == 1) {
beat(data.d.heartbeat_interval)
function beat(dur) {
lanyard.send(JSON.stringify({
op: 2,
d: {
subscribe_to_id: "534132311781015564"
}
op: 3
}))
} else if (data.op == 0) {
lanyardData = data
setTimeout(() => {
beat(dur)
}, dur);
}
for (let index = 0; index < sockets.length; index++) {
var socketData = sockets[index]
var socket = socketData.socket;
socket.send(JSON.stringify(lanyardData))
lanyard.addEventListener("message", async (res) => {
var data = JSON.parse(res.data)
if (data.op == 1) {
beat(data.d.heartbeat_interval)
lanyard.send(JSON.stringify({
op: 2,
d: {
subscribe_to_id: "534132311781015564"
}
}))
} else if (data.op == 0) {
lanyardData = data
if ((Date.now() - socketData.lastPing) > 120000) {
sockets.splice(index, 1)
for (let index = 0; index < sockets.length; index++) {
var socketData = sockets[index]
var socket = socketData.socket;
socket.send(JSON.stringify(lanyardData))
if ((Date.now() - socketData.lastPing) > 120000) {
sockets.splice(index, 1)
}
}
}
}
})
})
lanyard.on('close', () => {
console.log("Disconnected")
})
lanyard.on('close', () => {
console.log("Disconnected")
setTimeout(() => {
socketeer()
}, 60000)
})
}
// wsServer = new WebSocket.WebSocketServer({ port: 8090 })
wsServer = WebSocket.Server;
let server = require('http').createServer()
wsServer = new wsServer({
@ -101,9 +107,9 @@ wsServer.on("connection", function connection(socket) {
socket.send(JSON.stringify(lanyardData))
socket.send(`{ "op": 1 }`)
sockets.push({socket, lastPing: Date.now()})
sockets.push({ socket, lastPing: Date.now() })
})
server.listen(PORT, function() {
server.listen(PORT, function () {
console.log("API & Websocket now on port: " + PORT)
})