refactor: Use Tauri WebSocket

This commit is contained in:
MystiPanda
2024-01-14 17:30:18 +08:00
parent 99740c1324
commit ba7242a815
9 changed files with 142 additions and 47 deletions

View File

@@ -30,7 +30,7 @@ export const LayoutTraffic = () => {
useLogSetup();
const { connect, disconnect } = useWebsocket((event) => {
const data = JSON.parse(event.data) as ITrafficItem;
const data = JSON.parse(event) as ITrafficItem;
trafficRef.current?.appendData(data);
setTraffic(data);
});
@@ -52,7 +52,7 @@ export const LayoutTraffic = () => {
const memoryWs = useWebsocket(
(event) => {
setMemory(JSON.parse(event.data));
setMemory(JSON.parse(event));
},
{ onError: () => setMemory({ inuse: 0 }) }
);
@@ -63,7 +63,9 @@ export const LayoutTraffic = () => {
memoryWs.connect(
`ws://${server}/memory?token=${encodeURIComponent(secret)}`
);
return () => memoryWs.disconnect();
return () => {
memoryWs.disconnect();
};
}, [clashInfo, pageVisible, displayMemory]);
const [up, upUnit] = parseTraffic(traffic.up);

View File

@@ -16,7 +16,7 @@ export const useLogSetup = () => {
const setLogData = useSetRecoilState(atomLogData);
const { connect, disconnect } = useWebsocket((event) => {
const data = JSON.parse(event.data) as ILogItem;
const data = JSON.parse(event) as ILogItem;
const time = dayjs().format("MM-DD HH:mm:ss");
setLogData((l) => {
if (l.length >= MAX_LOG_NUM) l.shift();

View File

@@ -1,20 +1,19 @@
import { useRef } from "react";
export type WsMsgFn = (event: MessageEvent<any>) => void;
import WebSocket from "tauri-plugin-websocket-api";
export type WsMsgFn = (event: string) => void;
export interface WsOptions {
errorCount?: number; // default is 5
retryInterval?: number; // default is 2500
onError?: () => void;
onError?: (e: any) => void;
}
export const useWebsocket = (onMessage: WsMsgFn, options?: WsOptions) => {
const wsRef = useRef<WebSocket | null>(null);
const timerRef = useRef<any>(null);
const disconnect = () => {
const disconnect = async () => {
if (wsRef.current) {
wsRef.current.close();
await wsRef.current.disconnect();
wsRef.current = null;
}
if (timerRef.current) {
@@ -22,31 +21,37 @@ export const useWebsocket = (onMessage: WsMsgFn, options?: WsOptions) => {
}
};
const connect = (url: string) => {
const connect = async (url: string) => {
let errorCount = options?.errorCount ?? 5;
if (!url) return;
const connectHelper = async () => {
await disconnect();
const ws = await WebSocket.connect(url);
const connectHelper = () => {
disconnect();
const ws = new WebSocket(url);
wsRef.current = ws;
ws.addEventListener("message", onMessage);
ws.addEventListener("error", () => {
errorCount -= 1;
if (errorCount >= 0) {
timerRef.current = setTimeout(connectHelper, 2500);
} else {
disconnect();
options?.onError?.();
ws.addListener((event) => {
switch (event.type) {
case "Text": {
onMessage(event.data);
break;
}
default: {
break;
}
}
});
wsRef.current = ws;
};
connectHelper();
try {
await connectHelper();
} catch (e) {
errorCount -= 1;
if (errorCount >= 0) {
timerRef.current = setTimeout(connectHelper, 2500);
} else {
await disconnect();
options?.onError?.(e);
}
}
};
return { connect, disconnect };

View File

@@ -61,7 +61,7 @@ const ConnectionsPage = () => {
const { connect, disconnect } = useWebsocket(
(event) => {
// meta v1.15.0 出现data.connections为null的情况
const data = JSON.parse(event.data) as IConnections;
const data = JSON.parse(event) as IConnections;
// 尽量与前一次connections的展示顺序保持一致
setConnData((old) => {
const oldConn = old.connections;
@@ -94,7 +94,7 @@ const ConnectionsPage = () => {
return { ...data, connections };
});
},
{ errorCount: 3, retryInterval: 1000 }
{ errorCount: 3 }
);
useEffect(() => {