make ws more reliable
This commit is contained in:
parent
0614b52745
commit
373a6355a6
@ -22,30 +22,23 @@ type ScoresaberSocket = {
|
|||||||
*/
|
*/
|
||||||
export function connectScoreSaberWebSocket({ onMessage, onScore }: ScoresaberSocket) {
|
export function connectScoreSaberWebSocket({ onMessage, onScore }: ScoresaberSocket) {
|
||||||
let websocket: WebSocket | null = null;
|
let websocket: WebSocket | null = null;
|
||||||
let heartbeatInterval: NodeJS.Timeout | null = null; // To store heartbeat interval ID
|
|
||||||
|
|
||||||
function connectWs() {
|
function connectWs() {
|
||||||
websocket = new WebSocket("wss://scoresaber.com/ws");
|
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 => {
|
||||||
console.error("WebSocket Error:", error);
|
console.error("WebSocket Error:", error);
|
||||||
|
if (websocket) {
|
||||||
|
websocket.close(); // Close the connection on error
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
websocket.onclose = () => {
|
websocket.onclose = () => {
|
||||||
console.log("Lost connection to the ScoreSaber WebSocket. Attempting to reconnect...");
|
console.log("Lost connection to the ScoreSaber WebSocket. Attempting to reconnect...");
|
||||||
clearInterval(heartbeatInterval!); // Clear the heartbeat interval
|
|
||||||
setTimeout(connectWs, 5000); // Reconnect after 5 seconds
|
setTimeout(connectWs, 5000); // Reconnect after 5 seconds
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user