Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
fix: refresh websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzgydi committed Nov 25, 2022
1 parent 2ce7624 commit 376011e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/components/layout/layout-traffic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const LayoutTraffic = () => {
// setup log ws during layout
useLogSetup();

const [refresh, setRefresh] = useState({});

useEffect(() => {
if (!clashInfo) return;

Expand All @@ -33,8 +35,12 @@ const LayoutTraffic = () => {
setTraffic(data);
});

ws.addEventListener("error", () => {
setTimeout(() => setRefresh({}), 1000);
});

return () => ws?.close();
}, [clashInfo]);
}, [clashInfo, refresh]);

const [up, upUnit] = parseTraffic(traffic.up);
const [down, downUnit] = parseTraffic(traffic.down);
Expand Down
10 changes: 8 additions & 2 deletions src/components/layout/use-log-setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dayjs from "dayjs";
import { useEffect } from "react";
import { useEffect, useState } from "react";
import { useRecoilValue, useSetRecoilState } from "recoil";
import { getClashLogs } from "@/services/cmds";
import { useClashInfo } from "@/hooks/use-clash";
Expand All @@ -14,6 +14,8 @@ export const useLogSetup = () => {
const enableLog = useRecoilValue(atomEnableLog);
const setLogData = useSetRecoilState(atomLogData);

const [refresh, setRefresh] = useState({});

useEffect(() => {
if (!enableLog || !clashInfo) return;

Expand All @@ -31,6 +33,10 @@ export const useLogSetup = () => {
});
});

ws.addEventListener("error", () => {
setTimeout(() => setRefresh({}), 1000);
});

return () => ws?.close();
}, [clashInfo, enableLog]);
}, [clashInfo, enableLog, refresh]);
};

0 comments on commit 376011e

Please sign in to comment.