Skip to content

Commit 258b956

Browse files
committed
Reconnect websockets on error/close
1 parent 8e01dfa commit 258b956

File tree

6 files changed

+11
-6
lines changed

6 files changed

+11
-6
lines changed

frontend/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function App() {
3737
if (kind === "state") {
3838
setDoFixedSpeed(data.doFixedSpeed);
3939
} else if (kind === "error") {
40-
errorToast(data);
40+
errorToast("Unknown error");
4141
console.error(data);
4242
}
4343
}, [lastJsonMessage]);

frontend/src/containers/CPUTuning.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function CPUTuning() {
4141
} else if (kind === "success") {
4242
successToast("Successfully applied.");
4343
} else if (kind === "error") {
44-
errorToast(data);
44+
errorToast("Couldn't apply change.");
4545
console.error(data);
4646
}
4747
}, [lastJsonMessage]);

frontend/src/containers/FanTable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function FanTable({ disabled }: { disabled: boolean }) {
4747
} else if (kind === "success") {
4848
successToast("Successfully applied.");
4949
} else if (kind === "error") {
50-
errorToast(data);
50+
errorToast("Couldn't apply change.");
5151
console.error(data);
5252
}
5353
}, [lastJsonMessage]);

frontend/src/containers/FixedSpeed.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function FixedSpeed({ disabled }: { disabled: boolean }) {
3535
} else if (kind === "success") {
3636
successToast("Successfully applied.");
3737
} else if (kind === "error") {
38-
errorToast(data);
38+
errorToast("Couldn't apply change.");
3939
console.error(data);
4040
}
4141
}, [lastJsonMessage]);

frontend/src/containers/Toggles.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function Toggles() {
2626
});
2727
}
2828
} else if (kind === "error") {
29-
errorToast(data);
29+
errorToast("Couldn't apply change.");
3030
console.error(data);
3131
}
3232
}, [lastJsonMessage, sendJsonMessage]);

frontend/src/utils/hooks.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ import useReactWebSocket, { ReadyState } from "react-use-websocket";
22
import { MessageToClient } from "../../../common/types";
33

44
const emptyObject = {};
5+
const shouldReconnect = (_: CloseEvent) => true;
56

67
export function useWebSocket() {
78
const { lastJsonMessage, sendJsonMessage, readyState } =
8-
useReactWebSocket<MessageToClient | null>("ws://localhost:5523");
9+
useReactWebSocket<MessageToClient | null>("ws://localhost:5523", {
10+
retryOnError: true,
11+
reconnectAttempts: Number.MAX_SAFE_INTEGER,
12+
shouldReconnect,
13+
});
914

1015
return {
1116
lastJsonMessage:

0 commit comments

Comments
 (0)