make ws more reliable
All checks were successful
Deploy Backend / deploy (push) Successful in 3m29s
Deploy Website / deploy (push) Successful in 5m19s

This commit is contained in:
Lee 2024-10-17 18:01:34 +01:00
parent 0614b52745
commit 373a6355a6

View File

@ -22,30 +22,23 @@ type ScoresaberSocket = {
*/
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 => {
console.error("WebSocket Error:", error);
if (websocket) {
websocket.close(); // Close the connection on error
}
};
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
};