fix: websocket disconnect when window focus
This commit is contained in:
parent
54e491d8bf
commit
6f5acee1c3
@ -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";
|
|||||||
|
|
||||||
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);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user