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!")
|
// 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({
|
lanyard.send(JSON.stringify({
|
||||||
op: 3
|
op: 3
|
||||||
}))
|
}))
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
beat(dur)
|
beat(dur)
|
||||||
}, dur);
|
}, dur);
|
||||||
}
|
}
|
||||||
|
|
||||||
lanyard.addEventListener("message", async (res) => {
|
lanyard.addEventListener("message", async (res) => {
|
||||||
var data = JSON.parse(res.data)
|
var data = JSON.parse(res.data)
|
||||||
if (data.op == 1) {
|
if (data.op == 1) {
|
||||||
beat(data.d.heartbeat_interval)
|
beat(data.d.heartbeat_interval)
|
||||||
|
@ -68,13 +69,18 @@ lanyard.addEventListener("message", async (res) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
lanyard.on('close', () => {
|
lanyard.on('close', () => {
|
||||||
console.log("Disconnected")
|
console.log("Disconnected")
|
||||||
})
|
setTimeout(() => {
|
||||||
|
socketeer()
|
||||||
|
}, 60000)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// wsServer = new WebSocket.WebSocketServer({ port: 8090 })
|
// wsServer = new WebSocket.WebSocketServer({ port: 8090 })
|
||||||
|
|
||||||
wsServer = WebSocket.Server;
|
wsServer = WebSocket.Server;
|
||||||
let server = require('http').createServer()
|
let server = require('http').createServer()
|
||||||
wsServer = new wsServer({
|
wsServer = new wsServer({
|
||||||
|
@ -101,9 +107,9 @@ wsServer.on("connection", function connection(socket) {
|
||||||
socket.send(JSON.stringify(lanyardData))
|
socket.send(JSON.stringify(lanyardData))
|
||||||
socket.send(`{ "op": 1 }`)
|
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)
|
console.log("API & Websocket now on port: " + PORT)
|
||||||
})
|
})
|
Loading…
Reference in a new issue