Reconnect automatically
This commit is contained in:
parent
b11d5b51ed
commit
26b2bdc6e5
1 changed files with 42 additions and 36 deletions
24
index.js
24
index.js
|
@ -34,18 +34,19 @@ 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) {
|
||||
function beat(dur) {
|
||||
lanyard.send(JSON.stringify({
|
||||
op: 3
|
||||
}))
|
||||
setTimeout(() => {
|
||||
beat(dur)
|
||||
}, dur);
|
||||
}
|
||||
}
|
||||
|
||||
lanyard.addEventListener("message", async (res) => {
|
||||
lanyard.addEventListener("message", async (res) => {
|
||||
var data = JSON.parse(res.data)
|
||||
if (data.op == 1) {
|
||||
beat(data.d.heartbeat_interval)
|
||||
|
@ -68,13 +69,18 @@ lanyard.addEventListener("message", async (res) => {
|
|||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
lanyard.on('close', () => {
|
||||
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)
|
||||
})
|
Loading…
Reference in a new issue