Skip to content
Snippets Groups Projects
Unverified Commit 6f5acee1 authored by GyDi's avatar GyDi
Browse files

fix: websocket disconnect when window focus

parent 54e491d8
No related branches found
No related tags found
No related merge requests found
...@@ -8,10 +8,17 @@ export const useVisibility = () => { ...@@ -8,10 +8,17 @@ export const useVisibility = () => {
setVisible(document.visibilityState === "visible"); setVisible(document.visibilityState === "visible");
}; };
const handleFocus = () => setVisible(true);
const handleClick = () => setVisible(true);
handleVisibilityChange(); handleVisibilityChange();
document.addEventListener("focus", handleFocus);
document.addEventListener("pointerdown", handleClick);
document.addEventListener("visibilitychange", handleVisibilityChange); document.addEventListener("visibilitychange", handleVisibilityChange);
return () => { return () => {
document.removeEventListener("focus", handleFocus);
document.removeEventListener("pointerdown", handleClick);
document.removeEventListener("visibilitychange", handleVisibilityChange); document.removeEventListener("visibilitychange", handleVisibilityChange);
}; };
}, []); }, []);
......
...@@ -2,12 +2,12 @@ import { useRef } from "react"; ...@@ -2,12 +2,12 @@ import { useRef } from "react";
export type WsMsgFn = (event: MessageEvent<any>) => void; export type WsMsgFn = (event: MessageEvent<any>) => void;
interface Options { export interface WsOptions {
errorCount?: number; // default is 5 errorCount?: number; // default is 5
retryInterval?: number; // default is 2500 retryInterval?: number; // default is 2500
} }
export const useWebsocket = (onMessage: WsMsgFn, options?: Options) => { export const useWebsocket = (onMessage: WsMsgFn, options?: WsOptions) => {
const wsRef = useRef<WebSocket | null>(null); const wsRef = useRef<WebSocket | null>(null);
const timerRef = useRef<any>(null); const timerRef = useRef<any>(null);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment