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

Commit

Permalink
fix: reconnect websocket when restart clash
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzgydi committed Feb 21, 2022
1 parent 81ee989 commit 1a55cca
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/components/layout/layout-traffic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useEffect, useState } from "react";
import { useRecoilValue } from "recoil";
import { Box, Typography } from "@mui/material";
import { ArrowDownward, ArrowUpward } from "@mui/icons-material";
import { listen } from "@tauri-apps/api/event";
import { ApiType } from "../../services/types";
import { getInfomation } from "../../services/api";
import { getVergeConfig } from "../../services/cmds";
Expand All @@ -14,11 +15,21 @@ const LayoutTraffic = () => {
const portValue = useRecoilValue(atomClashPort);
const [traffic, setTraffic] = useState({ up: 0, down: 0 });
const { canvasRef, appendData, toggleStyle } = useTrafficGraph();
const [refresh, setRefresh] = useState({});

// whether hide traffic graph
const { data } = useSWR("getVergeConfig", getVergeConfig);
const trafficGraph = data?.traffic_graph ?? true;

useEffect(() => {
let unlisten: () => void = null!;

// should reconnect the traffic ws
listen("restart_clash", () => setRefresh({})).then((fn) => (unlisten = fn));

return () => unlisten?.();
}, []);

useEffect(() => {
let ws: WebSocket | null = null;

Expand All @@ -34,7 +45,7 @@ const LayoutTraffic = () => {
});

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

const [up, upUnit] = parseTraffic(traffic.up);
const [down, downUnit] = parseTraffic(traffic.down);
Expand Down

0 comments on commit 1a55cca

Please sign in to comment.