mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 23:03:44 +08:00
* feat: allow manual selection of url-test group * feat: fixed proxy indicator * fix: try to fix traffic websocket no longer updating * fixup: group delay test use defined url
61 lines
1.4 KiB
TypeScript
61 lines
1.4 KiB
TypeScript
import { useRef } from "react";
|
|
|
|
export type WsMsgFn = (event: MessageEvent<any>) => void;
|
|
|
|
export interface WsOptions {
|
|
errorCount?: number; // default is 5
|
|
retryInterval?: number; // default is 2500
|
|
onError?: (event: Event) => void;
|
|
onClose?: (event: CloseEvent) => void;
|
|
}
|
|
|
|
export const useWebsocket = (onMessage: WsMsgFn, options?: WsOptions) => {
|
|
const wsRef = useRef<WebSocket | null>(null);
|
|
const timerRef = useRef<any>(null);
|
|
|
|
const disconnect = () => {
|
|
if (wsRef.current) {
|
|
wsRef.current.close();
|
|
wsRef.current = null;
|
|
}
|
|
if (timerRef.current) {
|
|
clearTimeout(timerRef.current);
|
|
}
|
|
};
|
|
|
|
const connect = (url: string) => {
|
|
let errorCount = options?.errorCount ?? 5;
|
|
|
|
if (!url) return;
|
|
|
|
const connectHelper = () => {
|
|
disconnect();
|
|
|
|
const ws = new WebSocket(url);
|
|
wsRef.current = ws;
|
|
|
|
ws.addEventListener("message", (event) => {
|
|
errorCount = 0; // reset counter
|
|
onMessage(event);
|
|
});
|
|
ws.addEventListener("error", (event) => {
|
|
errorCount -= 1;
|
|
|
|
if (errorCount >= 0) {
|
|
timerRef.current = setTimeout(connectHelper, 2500);
|
|
} else {
|
|
disconnect();
|
|
options?.onError?.(event);
|
|
}
|
|
});
|
|
ws.addEventListener("close", (event) => {
|
|
options?.onClose?.(event);
|
|
});
|
|
};
|
|
|
|
connectHelper();
|
|
};
|
|
|
|
return { connect, disconnect };
|
|
};
|