refactor: connect mihomo websocket

This commit is contained in:
oomeow
2025-10-04 10:53:44 +08:00
Unverified
parent f92b3527de
commit 04bbe7f544
15 changed files with 153 additions and 245 deletions

View File

@@ -1,4 +1,3 @@
import { listen } from "@tauri-apps/api/event";
import { useLocalStorage } from "foxact/use-local-storage";
import { useEffect, useRef } from "react";
import { mutate } from "swr";
@@ -17,28 +16,28 @@ export const useConnectionData = () => {
const ws = useRef<MihomoWebSocket | null>(null);
const wsFirstConnection = useRef<boolean>(true);
const listenerRef = useRef<() => void | null>(null);
const timeoutRef = useRef<ReturnType<typeof setTimeout>>(null);
const response = useSWRSubscription<IConnections, any, string | null>(
subscriptKey,
(_key, { next }) => {
const reconnect = async () => {
await ws.current?.close();
ws.current = null;
timeoutRef.current = setTimeout(async () => await connect(), 500);
};
const connect = () =>
MihomoWebSocket.connect_connections()
.then((ws_) => {
ws.current = ws_;
if (timeoutRef.current) clearTimeout(timeoutRef.current);
listenerRef.current = ws_.addListener(async (msg) => {
ws_.addListener(async (msg) => {
if (msg.type === "Text") {
if (msg.data.startsWith("Websocket error")) {
next(msg.data);
await ws.current?.close();
ws.current = null;
timeoutRef.current = setTimeout(
async () => await connect(),
500,
);
await reconnect();
} else {
const data = JSON.parse(msg.data) as IConnections;
next(null, (old = initConnData) => {
@@ -89,8 +88,6 @@ export const useConnectionData = () => {
return () => {
ws.current?.close();
listenerRef.current?.();
listenerRef.current = null;
};
},
{
@@ -99,20 +96,6 @@ export const useConnectionData = () => {
},
);
useEffect(() => {
const unlistenRefreshWebsocket = listen(
"verge://refresh-websocket",
async () => {
await ws.current?.close();
setDate(Date.now());
},
);
return () => {
unlistenRefreshWebsocket.then((fn) => fn());
};
}, [setDate]);
useEffect(() => {
mutate(`$sub$${subscriptKey}`);
}, [date, subscriptKey]);

View File

@@ -1,19 +1,16 @@
// import { getClashLogs } from "@/services/cmds";
import { listen } from "@tauri-apps/api/event";
import dayjs from "dayjs";
import { useLocalStorage } from "foxact/use-local-storage";
import { useEffect, useRef } from "react";
import { mutate } from "swr";
import useSWRSubscription from "swr/subscription";
import { MihomoWebSocket } from "tauri-plugin-mihomo-api";
import { useClashLog } from "@/services/states";
import { getClashLogs } from "@/services/cmds";
import { useClashLog } from "@/services/states";
const MAX_LOG_NUM = 1000;
export const useLogData = () => {
// const [enableLog] = useEnableLog();
// const [logLevel] = useLocalStorage<LogLevel>("log:log-level", "info");
const [clashLog] = useClashLog();
const enableLog = clashLog.enable;
const logLevel = clashLog.logLevel;
@@ -23,12 +20,17 @@ export const useLogData = () => {
const ws = useRef<MihomoWebSocket | null>(null);
const wsFirstConnection = useRef<boolean>(true);
const listenerRef = useRef<() => void | null>(null);
const timeoutRef = useRef<ReturnType<typeof setTimeout>>(null);
const response = useSWRSubscription<ILogItem[], any, string | null>(
subscriptKey,
(_key, { next }) => {
const reconnect = async () => {
await ws.current?.close();
ws.current = null;
timeoutRef.current = setTimeout(async () => await connect(), 500);
};
const connect = () =>
MihomoWebSocket.connect_logs(logLevel)
.then(async (ws_) => {
@@ -81,16 +83,11 @@ export const useLogData = () => {
}
flushTimer = null;
};
listenerRef.current = ws_.addListener(async (msg) => {
ws_.addListener(async (msg) => {
if (msg.type === "Text") {
if (msg.data.startsWith("Websocket error")) {
next(msg.data);
await ws.current?.close();
ws.current = null;
timeoutRef.current = setTimeout(
async () => await connect(),
500,
);
await reconnect();
} else {
const data = JSON.parse(msg.data) as ILogItem;
data.time = dayjs().format("MM-DD HH:mm:ss");
@@ -124,8 +121,6 @@ export const useLogData = () => {
return () => {
ws.current?.close();
listenerRef.current?.();
listenerRef.current = null;
};
},
{
@@ -134,20 +129,6 @@ export const useLogData = () => {
},
);
useEffect(() => {
const unlistenRefreshWebsocket = listen(
"verge://refresh-websocket",
async () => {
await ws.current?.close();
setDate(Date.now());
},
);
return () => {
unlistenRefreshWebsocket.then((fn) => fn());
};
}, [setDate]);
useEffect(() => {
mutate(`$sub$${subscriptKey}`);
}, [date, subscriptKey]);

View File

@@ -1,4 +1,3 @@
import { listen } from "@tauri-apps/api/event";
import { useLocalStorage } from "foxact/use-local-storage";
import { useEffect, useRef } from "react";
import { mutate } from "swr";
@@ -16,28 +15,28 @@ export const useMemoryData = () => {
const ws = useRef<MihomoWebSocket | null>(null);
const wsFirstConnection = useRef<boolean>(true);
const listenerRef = useRef<() => void | null>(null);
const timeoutRef = useRef<ReturnType<typeof setTimeout>>(null);
const response = useSWRSubscription<IMemoryUsageItem, any, string | null>(
subscriptKey,
(_key, { next }) => {
const reconnect = async () => {
await ws.current?.close();
ws.current = null;
timeoutRef.current = setTimeout(async () => await connect(), 500);
};
const connect = () =>
MihomoWebSocket.connect_memory()
.then((ws_) => {
ws.current = ws_;
if (timeoutRef.current) clearTimeout(timeoutRef.current);
listenerRef.current = ws_.addListener(async (msg) => {
ws_.addListener(async (msg) => {
if (msg.type === "Text") {
if (msg.data.startsWith("Websocket error")) {
next(msg.data, { inuse: 0 });
await ws.current?.close();
ws.current = null;
timeoutRef.current = setTimeout(
async () => await connect(),
500,
);
await reconnect();
} else {
const data = JSON.parse(msg.data) as IMemoryUsageItem;
next(null, data);
@@ -65,8 +64,6 @@ export const useMemoryData = () => {
return () => {
ws.current?.close();
listenerRef.current?.();
listenerRef.current = null;
};
},
{
@@ -75,20 +72,6 @@ export const useMemoryData = () => {
},
);
useEffect(() => {
const unlistenRefreshWebsocket = listen(
"verge://refresh-websocket",
async () => {
await ws.current?.close();
setDate(Date.now());
},
);
return () => {
unlistenRefreshWebsocket.then((fn) => fn());
};
}, [setDate]);
useEffect(() => {
mutate(`$sub$${subscriptKey}`);
}, [date, subscriptKey]);

View File

@@ -1,11 +1,11 @@
import { TrafficRef } from "@/components/layout/traffic-graph";
import { listen } from "@tauri-apps/api/event";
import { useLocalStorage } from "foxact/use-local-storage";
import { useEffect, useRef } from "react";
import { mutate } from "swr";
import useSWRSubscription from "swr/subscription";
import { MihomoWebSocket } from "tauri-plugin-mihomo-api";
import { TrafficRef } from "@/components/layout/traffic-graph";
export const useTrafficData = () => {
const [date, setDate] = useLocalStorage("mihomo_traffic_date", Date.now());
const subscriptKey = `getClashTraffic-${date}`;
@@ -13,28 +13,28 @@ export const useTrafficData = () => {
const trafficRef = useRef<TrafficRef>(null);
const ws = useRef<MihomoWebSocket | null>(null);
const wsFirstConnection = useRef<boolean>(true);
const listenerRef = useRef<() => void | null>(null);
const timeoutRef = useRef<ReturnType<typeof setTimeout>>(null);
const response = useSWRSubscription<ITrafficItem, any, string | null>(
subscriptKey,
(_key, { next }) => {
const reconnect = async () => {
await ws.current?.close();
ws.current = null;
timeoutRef.current = setTimeout(async () => await connect(), 500);
};
const connect = async () => {
MihomoWebSocket.connect_traffic()
.then(async (ws_) => {
ws.current = ws_;
if (timeoutRef.current) clearTimeout(timeoutRef.current);
listenerRef.current = ws_.addListener(async (msg) => {
ws_.addListener(async (msg) => {
if (msg.type === "Text") {
if (msg.data.startsWith("Websocket error")) {
next(msg.data, { up: 0, down: 0 });
await ws.current?.close();
ws.current = null;
timeoutRef.current = setTimeout(
async () => await connect(),
500,
);
await reconnect();
} else {
const data = JSON.parse(msg.data) as ITrafficItem;
trafficRef.current?.appendData(data);
@@ -64,8 +64,6 @@ export const useTrafficData = () => {
return () => {
ws.current?.close();
listenerRef.current?.();
listenerRef.current = null;
};
},
{
@@ -74,20 +72,6 @@ export const useTrafficData = () => {
},
);
useEffect(() => {
const unlistenRefreshWebsocket = listen(
"verge://refresh-websocket",
async () => {
await ws.current?.close();
setDate(Date.now());
},
);
return () => {
unlistenRefreshWebsocket.then((fn) => fn());
};
}, [setDate]);
useEffect(() => {
mutate(`$sub$${subscriptKey}`);
}, [date, subscriptKey]);