send a heartbeat to the scoresaber ws
This commit is contained in:
parent
c72230a98d
commit
0614b52745
@ -19,8 +19,8 @@
|
||||
"typescript": "^5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@typegoose/typegoose": "^12.8.0",
|
||||
"ky": "^1.7.2",
|
||||
"ws": "^8.18.0",
|
||||
"@typegoose/typegoose": "^12.8.0"
|
||||
"ws": "^8.18.0"
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
import WebSocket from "ws";
|
||||
import ScoreSaberPlayerScoreToken from "../types/token/scoresaber/score-saber-player-score-token";
|
||||
|
||||
type ScoresaberWebsocket = {
|
||||
type ScoresaberSocket = {
|
||||
/**
|
||||
* Invoked when a general message is received.
|
||||
*
|
||||
@ -18,13 +18,25 @@ type ScoresaberWebsocket = {
|
||||
};
|
||||
|
||||
/**
|
||||
* Connects to the ScoreSaber WebSocket and handles incoming messages.
|
||||
* Connects to the ScoreSaber websocket and handles incoming messages.
|
||||
*/
|
||||
export function connectScoreSaberWebSocket({ onMessage, onScore }: ScoresaberWebsocket) {
|
||||
let websocket = connectWs();
|
||||
export function connectScoreSaberWebSocket({ onMessage, onScore }: ScoresaberSocket) {
|
||||
let websocket: WebSocket | null = null;
|
||||
let heartbeatInterval: NodeJS.Timeout | null = null; // To store heartbeat interval ID
|
||||
|
||||
function connectWs() {
|
||||
websocket = new WebSocket("wss://scoresaber.com/ws");
|
||||
|
||||
websocket.onopen = () => {
|
||||
console.log("Connected to the ScoreSaber WebSocket!");
|
||||
|
||||
// Start sending heartbeats
|
||||
heartbeatInterval = setInterval(() => {
|
||||
if (websocket && websocket.readyState === WebSocket.OPEN) {
|
||||
websocket.send(JSON.stringify({ type: "heartbeat" })); // Send heartbeat message
|
||||
console.log("Heartbeat sent");
|
||||
}
|
||||
}, 30000); // Sends a heartbeat every 30 seconds
|
||||
};
|
||||
|
||||
websocket.onerror = error => {
|
||||
@ -32,10 +44,9 @@ export function connectScoreSaberWebSocket({ onMessage, onScore }: ScoresaberWeb
|
||||
};
|
||||
|
||||
websocket.onclose = () => {
|
||||
console.log("Lost connection to the ScoreSaber WebSocket. Reconnecting in 5 seconds...");
|
||||
setTimeout(() => {
|
||||
websocket = connectWs();
|
||||
}, 5000);
|
||||
console.log("Lost connection to the ScoreSaber WebSocket. Attempting to reconnect...");
|
||||
clearInterval(heartbeatInterval!); // Clear the heartbeat interval
|
||||
setTimeout(connectWs, 5000); // Reconnect after 5 seconds
|
||||
};
|
||||
|
||||
websocket.onmessage = messageEvent => {
|
||||
@ -55,10 +66,5 @@ export function connectScoreSaberWebSocket({ onMessage, onScore }: ScoresaberWeb
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes and returns a new WebSocket connection to ScoreSaber.
|
||||
*/
|
||||
function connectWs(): WebSocket {
|
||||
console.log("Connecting to the ScoreSaber WebSocket...");
|
||||
return new WebSocket("wss://scoresaber.com/ws");
|
||||
connectWs(); // Initiate the first connection
|
||||
}
|
||||
|
Reference in New Issue
Block a user