send a heartbeat to the scoresaber ws
This commit is contained in:
parent
c72230a98d
commit
0614b52745
@ -19,8 +19,8 @@
|
|||||||
"typescript": "^5"
|
"typescript": "^5"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@typegoose/typegoose": "^12.8.0",
|
||||||
"ky": "^1.7.2",
|
"ky": "^1.7.2",
|
||||||
"ws": "^8.18.0",
|
"ws": "^8.18.0"
|
||||||
"@typegoose/typegoose": "^12.8.0"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import WebSocket from "ws";
|
import WebSocket from "ws";
|
||||||
import ScoreSaberPlayerScoreToken from "../types/token/scoresaber/score-saber-player-score-token";
|
import ScoreSaberPlayerScoreToken from "../types/token/scoresaber/score-saber-player-score-token";
|
||||||
|
|
||||||
type ScoresaberWebsocket = {
|
type ScoresaberSocket = {
|
||||||
/**
|
/**
|
||||||
* Invoked when a general message is received.
|
* 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) {
|
export function connectScoreSaberWebSocket({ onMessage, onScore }: ScoresaberSocket) {
|
||||||
let websocket = connectWs();
|
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 = () => {
|
websocket.onopen = () => {
|
||||||
console.log("Connected to the ScoreSaber WebSocket!");
|
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 => {
|
websocket.onerror = error => {
|
||||||
@ -32,10 +44,9 @@ export function connectScoreSaberWebSocket({ onMessage, onScore }: ScoresaberWeb
|
|||||||
};
|
};
|
||||||
|
|
||||||
websocket.onclose = () => {
|
websocket.onclose = () => {
|
||||||
console.log("Lost connection to the ScoreSaber WebSocket. Reconnecting in 5 seconds...");
|
console.log("Lost connection to the ScoreSaber WebSocket. Attempting to reconnect...");
|
||||||
setTimeout(() => {
|
clearInterval(heartbeatInterval!); // Clear the heartbeat interval
|
||||||
websocket = connectWs();
|
setTimeout(connectWs, 5000); // Reconnect after 5 seconds
|
||||||
}, 5000);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
websocket.onmessage = messageEvent => {
|
websocket.onmessage = messageEvent => {
|
||||||
@ -53,12 +64,7 @@ export function connectScoreSaberWebSocket({ onMessage, onScore }: ScoresaberWeb
|
|||||||
console.warn("Received invalid message:", messageEvent.data);
|
console.warn("Received invalid message:", messageEvent.data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
connectWs(); // Initiate the first connection
|
||||||
* 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");
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user