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,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) {
|
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
|
||||||
|
|
||||||
websocket.onopen = () => {
|
function connectWs() {
|
||||||
console.log("Connected to the ScoreSaber WebSocket!");
|
websocket = new WebSocket("wss://scoresaber.com/ws");
|
||||||
};
|
|
||||||
|
|
||||||
websocket.onerror = error => {
|
websocket.onopen = () => {
|
||||||
console.error("WebSocket Error:", error);
|
console.log("Connected to the ScoreSaber WebSocket!");
|
||||||
};
|
|
||||||
|
|
||||||
websocket.onclose = () => {
|
// Start sending heartbeats
|
||||||
console.log("Lost connection to the ScoreSaber WebSocket. Reconnecting in 5 seconds...");
|
heartbeatInterval = setInterval(() => {
|
||||||
setTimeout(() => {
|
if (websocket && websocket.readyState === WebSocket.OPEN) {
|
||||||
websocket = connectWs();
|
websocket.send(JSON.stringify({ type: "heartbeat" })); // Send heartbeat message
|
||||||
}, 5000);
|
console.log("Heartbeat sent");
|
||||||
};
|
}
|
||||||
|
}, 30000); // Sends a heartbeat every 30 seconds
|
||||||
|
};
|
||||||
|
|
||||||
websocket.onmessage = messageEvent => {
|
websocket.onerror = error => {
|
||||||
if (typeof messageEvent.data !== "string") return;
|
console.error("WebSocket Error:", error);
|
||||||
|
};
|
||||||
|
|
||||||
try {
|
websocket.onclose = () => {
|
||||||
const command = JSON.parse(messageEvent.data);
|
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") {
|
websocket.onmessage = messageEvent => {
|
||||||
onScore && onScore(command.commandData as ScoreSaberPlayerScoreToken);
|
if (typeof messageEvent.data !== "string") return;
|
||||||
} else {
|
|
||||||
onMessage && onMessage(command);
|
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);
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
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