-1

I'm using SpeedTest package from '@cloudflare/speedtest' in my typescript Reactjs project. My code is -

 //Network Speed Detect
  const handleClickOpen = () => {
    const speedTest: any = new SpeedTest({
      measurements: [
        { type: "latency", numPackets: 1 }, // initial latency estimation
        { type: "download", bytes: 1e5, count: 1, bypassMinDuration: true }, // initial download estimation
        { type: "latency", numPackets: 20 },
        { type: "download", bytes: 1e5, count: 9 },
        { type: "download", bytes: 1e6, count: 8 },
        { type: "download", bytes: 1e7, count: 6 },
        { type: "download", bytes: 2.5e7, count: 4 },
        { type: "download", bytes: 1e8, count: 3 },
        { type: "download", bytes: 2.5e8, count: 2 },
      ],
    });
    if (speedTest.isRunning) {
      dispatch(setLoaderNetwork(true));
      const intervalId = setInterval(() => {
        const currentResult = speedTest.results;
        const downloadSpeed: any = (
          currentResult.getSummary().download / 1000000
        ).toFixed(2);
        if (downloadSpeed !== "NaN") {
          dispatch(setSpeed(downloadSpeed));
        }
        const latencySpeed: any = currentResult.getSummary().latency.toFixed(2);
        if (latencySpeed !== "NaN") {
          dispatch(setLatency(latencySpeed));
        }
        if (!speedTest.isRunning) {
          clearInterval(intervalId);
        }
      }, 500);
    }

    speedTest.onFinish = (results) => {
      dispatch(setLoaderNetwork(false));
      dispatch(
        setSpeed(Number((results.getSummary().download / 1000000).toFixed(2)))
      );
      dispatch(setLatency(Number(results.getSummary().latency.toFixed(2))));
    };
    setOpen(true);
  };

  const handleNetworkSpeedClose = () => {
    setOpen(false);
  };

It's running perfectly but the problem is after 10-15 minutes it stops working. It gives 0.00 mbps and when I checked in network console the data is getting download for around 22 mb and then it just stops with no error. Works fine if I refresh the page and data download is around 100mb when it gives correct data. Can't find any solution for it plz help

Checked network tab no api is giving any error. It works fine when I refresh the page.

0 Answers0