mirror of
https://github.com/Xevion/dynamic-preauth.git
synced 2025-12-06 03:14:58 -06:00
Setup websocket reconnection logic
This commit is contained in:
@@ -25,6 +25,7 @@ function useSocket(): UseSocketResult {
|
|||||||
const [downloads, setDownloads] = useState<Download[] | null>(null);
|
const [downloads, setDownloads] = useState<Download[] | null>(null);
|
||||||
const [executables, setExecutables] = useState<Executable[] | null>(null);
|
const [executables, setExecutables] = useState<Executable[] | null>(null);
|
||||||
const socketRef = useRef<WebSocket | null>(null);
|
const socketRef = useRef<WebSocket | null>(null);
|
||||||
|
const allowReconnectRef = useRef<boolean>(true);
|
||||||
|
|
||||||
function deleteDownload(download_token: number) {
|
function deleteDownload(download_token: number) {
|
||||||
if (socketRef.current == null) {
|
if (socketRef.current == null) {
|
||||||
@@ -43,6 +44,7 @@ function useSocket(): UseSocketResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
function connect() {
|
||||||
const socket = new WebSocket(
|
const socket = new WebSocket(
|
||||||
(window.location.protocol === "https:" ? "wss://" : "ws://") +
|
(window.location.protocol === "https:" ? "wss://" : "ws://") +
|
||||||
(import.meta.env.DEV ? "localhost:5800" : window.location.host) +
|
(import.meta.env.DEV ? "localhost:5800" : window.location.host) +
|
||||||
@@ -71,12 +73,27 @@ function useSocket(): UseSocketResult {
|
|||||||
|
|
||||||
socket.onclose = (event) => {
|
socket.onclose = (event) => {
|
||||||
console.log("WebSocket connection closed", event);
|
console.log("WebSocket connection closed", event);
|
||||||
|
|
||||||
|
socketRef.current = null;
|
||||||
|
if (allowReconnectRef.current) {
|
||||||
|
setId(null);
|
||||||
|
setDownloads(null);
|
||||||
|
setExecutables(null);
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
connect();
|
||||||
|
}, 3000);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
connect();
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
// Close the socket when the component is unmounted
|
// Close the socket when the component is unmounted
|
||||||
console.log("Unmounting, closing WebSocket connection");
|
console.log("Unmounting, closing WebSocket connection");
|
||||||
socket.close();
|
socketRef.current?.close();
|
||||||
|
allowReconnectRef.current = false;
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user