Made socket reconnect on connection loss or fail

This commit is contained in:
Liam 2022-10-29 10:38:40 +01:00
parent dc8fb42766
commit cd8f5adce3
2 changed files with 12 additions and 25 deletions

@ -1,4 +1,5 @@
import { w3cwebsocket as W3CWebSocket } from "websocket"; import { w3cwebsocket as W3CWebSocket } from "websocket";
import { useDataStore } from "../store/overlayDataStore";
import { useSettingsStore } from "../store/overlaySettingsStore"; import { useSettingsStore } from "../store/overlaySettingsStore";
import { usePlayerDataStore } from "../store/playerDataStore"; import { usePlayerDataStore } from "../store/playerDataStore";
import { useSongDataStore } from "../store/songDataStore"; import { useSongDataStore } from "../store/songDataStore";
@ -6,7 +7,6 @@ import Utils from "../utils/utils";
import { getMapHashFromLevelId } from "./map/mapHelpers"; import { getMapHashFromLevelId } from "./map/mapHelpers";
const ip = useSettingsStore.getState().socketAddr; const ip = useSettingsStore.getState().socketAddr;
let hasConnected = false;
let cutData: any = []; let cutData: any = [];
cutData.saberA = { cutData.saberA = {
@ -20,33 +20,21 @@ cutData.saberB = {
const updatePlayerData = usePlayerDataStore.getState().updatePlayerData; const updatePlayerData = usePlayerDataStore.getState().updatePlayerData;
export function connectClient() { export function connectClient(attempt: number = 1) {
const client = new W3CWebSocket(`ws://${ip}:6557/socket`); const client = new W3CWebSocket(`ws://${ip}:6557/socket`);
const retryTime = 30_000 * attempt > 60_000 ? 60_000 : 30_000 * attempt;
if (hasConnected) {
return;
}
client.onopen = () => { client.onopen = () => {
console.log("WebSocket Client Connected"); console.log("WebSocket Client Connected");
hasConnected = true;
}; };
client.onclose = () => { client.onclose = () => {
if (hasConnected) {
console.log( console.log(
"Lost connection to HTTPSiraStatus, attempting to reconnect." `Unable to connect to HTTPSiraStatus, retrying in ${retryTime} seconds. (Attempt: ${attempt})`
);
connectClient();
} else {
hasConnected = false;
console.log(
"Unable to connect to HTTPSiraStatus, retrying in 30 seconds."
); );
setTimeout(() => { setTimeout(() => {
connectClient(); connectClient(attempt + 1);
}, 30_000); }, retryTime);
}
}; };
client.onmessage = (message: any) => { client.onmessage = (message: any) => {
const data: string = message.data; const data: string = message.data;
@ -83,6 +71,7 @@ const handlers: any = {
} = data.status.beatmap; } = data.status.beatmap;
state.reset(); state.reset();
state.setInSong(true); state.setInSong(true);
useDataStore.setState({ loadedDuringSong: true });
state.updateMapData( state.updateMapData(
getMapHashFromLevelId(levelId), getMapHashFromLevelId(levelId),
difficultyEnum, difficultyEnum,
@ -187,12 +176,14 @@ const handlers: any = {
state.reset(); state.reset();
state.setInSong(false); state.setInSong(false);
updatePlayerData(); updatePlayerData();
useDataStore.setState({ loadedDuringSong: false });
}, },
menu: () => { menu: () => {
const state = useSongDataStore.getState(); const state = useSongDataStore.getState();
state.reset(); state.reset();
state.setInSong(false); state.setInSong(false);
updatePlayerData(); updatePlayerData();
useDataStore.setState({ loadedDuringSong: false });
}, },
softFail: () => { softFail: () => {
const state = useSongDataStore.getState(); const state = useSongDataStore.getState();

@ -11,10 +11,6 @@
letter-spacing: normal; letter-spacing: normal;
} }
.loading {
color: black;
}
.invalidPlayer { .invalidPlayer {
display: flex; display: flex;
flex-direction: column; flex-direction: column;