send a heartbeat to the scoresaber ws
All checks were successful
Deploy Backend / deploy (push) Successful in 3m8s
Deploy Website / deploy (push) Successful in 4m55s

This commit is contained in:
Lee 2024-10-17 17:46:57 +01:00
parent c72230a98d
commit 0614b52745
2 changed files with 43 additions and 37 deletions

@ -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,47 +18,53 @@ 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
websocket.onopen = () => {
console.log("Connected to the ScoreSaber WebSocket!");
};
function connectWs() {
websocket = new WebSocket("wss://scoresaber.com/ws");
websocket.onerror = error => {
console.error("WebSocket Error:", error);
};
websocket.onopen = () => {
console.log("Connected to the ScoreSaber WebSocket!");
websocket.onclose = () => {
console.log("Lost connection to the ScoreSaber WebSocket. Reconnecting in 5 seconds...");
setTimeout(() => {
websocket = connectWs();
}, 5000);
};
// 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.onmessage = messageEvent => {
if (typeof messageEvent.data !== "string") return;
websocket.onerror = error => {
console.error("WebSocket Error:", error);
};
try {
const command = JSON.parse(messageEvent.data);
websocket.onclose = () => {
console.log("Lost connection to the ScoreSaber WebSocket. Attempting to reconnect...");
clearInterval(heartbeatInterval!); // Clear the heartbeat interval
setTimeout(connectWs, 5000); // Reconnect after 5 seconds
};
if (command.commandName === "score") {
onScore && onScore(command.commandData as ScoreSaberPlayerScoreToken);
} else {
onMessage && onMessage(command);
websocket.onmessage = messageEvent => {
if (typeof messageEvent.data !== "string") return;
try {
const command = JSON.parse(messageEvent.data);
if (command.commandName === "score") {
onScore && onScore(command.commandData as ScoreSaberPlayerScoreToken);
} else {
onMessage && onMessage(command);
}
} catch (err) {
console.warn("Received invalid message:", messageEvent.data);
}
} catch (err) {
console.warn("Received invalid message:", messageEvent.data);
}
};
}
};
}
/**
* 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
}