feat(traffic): api support & adjust
This commit is contained in:
parent
0028bef559
commit
0a3c59450b
@ -1,29 +1,32 @@
|
|||||||
import axios from "axios";
|
import { CancelTokenSource } from "axios";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
import { Box, Typography } from "@mui/material";
|
||||||
import { ArrowDownward, ArrowUpward } from "@mui/icons-material";
|
import { ArrowDownward, ArrowUpward } from "@mui/icons-material";
|
||||||
import parseTraffic from "../utils/parse-traffic";
|
import parseTraffic from "../utils/parse-traffic";
|
||||||
import { Typography } from "@mui/material";
|
import services from "../services";
|
||||||
import { Box } from "@mui/system";
|
|
||||||
|
|
||||||
const Traffic = () => {
|
const Traffic = () => {
|
||||||
const [traffic, setTraffic] = useState({ up: 0, down: 0 });
|
const [traffic, setTraffic] = useState({ up: 0, down: 0 });
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const onTraffic = () => {
|
let timer: any = null;
|
||||||
axios({
|
let source: CancelTokenSource | null = null;
|
||||||
url: `http://127.0.0.1:9090/traffic`,
|
|
||||||
method: "GET",
|
async function onTraffic() {
|
||||||
onDownloadProgress: (progressEvent) => {
|
timer = null;
|
||||||
const data = progressEvent.currentTarget.response || "";
|
|
||||||
const lastData = data.slice(data.trim().lastIndexOf("\n") + 1);
|
|
||||||
try {
|
try {
|
||||||
if (lastData) setTraffic(JSON.parse(lastData));
|
source = await services.getTraffic(setTraffic);
|
||||||
} catch {}
|
} catch {
|
||||||
},
|
timer = setTimeout(onTraffic, 500);
|
||||||
}).catch(() => setTimeout(onTraffic, 500));
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
onTraffic();
|
onTraffic();
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (timer) clearTimeout(timer);
|
||||||
|
source?.cancel();
|
||||||
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const [up, upUnit] = parseTraffic(traffic.up);
|
const [up, upUnit] = parseTraffic(traffic.up);
|
||||||
@ -45,7 +48,7 @@ const Traffic = () => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Box width="110px">
|
<Box width="110px">
|
||||||
<Box mb={2} display="flex" alignItems="center" whiteSpace="nowrap">
|
<Box mb={1.5} display="flex" alignItems="center" whiteSpace="nowrap">
|
||||||
<ArrowUpward
|
<ArrowUpward
|
||||||
fontSize="small"
|
fontSize="small"
|
||||||
color={+up > 0 ? "primary" : "disabled"}
|
color={+up > 0 ? "primary" : "disabled"}
|
||||||
|
11
src/services/base.ts
Normal file
11
src/services/base.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import axios from "axios";
|
||||||
|
|
||||||
|
const axiosIns = axios.create({
|
||||||
|
baseURL: "http://127.0.0.1:9090",
|
||||||
|
});
|
||||||
|
|
||||||
|
axiosIns.interceptors.response.use((respone) => {
|
||||||
|
return respone.data;
|
||||||
|
});
|
||||||
|
|
||||||
|
export default axiosIns;
|
5
src/services/index.ts
Normal file
5
src/services/index.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import * as traffic from "./traffic";
|
||||||
|
|
||||||
|
export default {
|
||||||
|
...traffic,
|
||||||
|
};
|
29
src/services/traffic.ts
Normal file
29
src/services/traffic.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import axios from "axios";
|
||||||
|
import axiosIns from "./base";
|
||||||
|
|
||||||
|
export interface TrafficData {
|
||||||
|
up: number;
|
||||||
|
down: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the traffic stream
|
||||||
|
export async function getTraffic(callback: (data: TrafficData) => void) {
|
||||||
|
const source = axios.CancelToken.source();
|
||||||
|
|
||||||
|
axiosIns.get("/traffic", {
|
||||||
|
cancelToken: source.token,
|
||||||
|
onDownloadProgress: (progressEvent) => {
|
||||||
|
const data = progressEvent.currentTarget.response || "";
|
||||||
|
const lastData = data.slice(data.trim().lastIndexOf("\n") + 1);
|
||||||
|
|
||||||
|
if (!lastData) callback({ up: 0, down: 0 });
|
||||||
|
try {
|
||||||
|
callback(JSON.parse(lastData) as TrafficData);
|
||||||
|
} catch {
|
||||||
|
callback({ up: 0, down: 0 });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return source;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user