瀏覽代碼

simulate ping every 5 sec

Aakansha Doshi 4 年之前
父節點
當前提交
69a1b74e05
共有 6 個文件被更改,包括 53 次插入13 次删除
  1. 2 0
      src/appState.ts
  2. 30 13
      src/components/App.tsx
  3. 4 0
      src/components/Stats.tsx
  4. 1 0
      src/locales/en.json
  5. 15 0
      src/networkStats.ts
  6. 1 0
      src/types.ts

+ 2 - 0
src/appState.ts

@@ -74,6 +74,7 @@ export const getDefaultAppState = (): Omit<
     zoom: { value: 1 as NormalizedZoomValue, translation: { x: 0, y: 0 } },
     zoom: { value: 1 as NormalizedZoomValue, translation: { x: 0, y: 0 } },
     viewModeEnabled: false,
     viewModeEnabled: false,
     networkSpeed: 0,
     networkSpeed: 0,
+    ping: "…",
   };
   };
 };
 };
 
 
@@ -155,6 +156,7 @@ const APP_STATE_STORAGE_CONF = (<
   zoom: { browser: true, export: false },
   zoom: { browser: true, export: false },
   viewModeEnabled: { browser: false, export: false },
   viewModeEnabled: { browser: false, export: false },
   networkSpeed: { browser: false, export: false },
   networkSpeed: { browser: false, export: false },
+  ping: { browser: false, export: false },
 });
 });
 
 
 const _clearAppStateForStorage = <ExportType extends "export" | "browser">(
 const _clearAppStateForStorage = <ExportType extends "export" | "browser">(

+ 30 - 13
src/components/App.tsx

@@ -184,7 +184,7 @@ import LayerUI from "./LayerUI";
 import { Stats } from "./Stats";
 import { Stats } from "./Stats";
 import { Toast } from "./Toast";
 import { Toast } from "./Toast";
 import { actionToggleViewMode } from "../actions/actionToggleViewMode";
 import { actionToggleViewMode } from "../actions/actionToggleViewMode";
-import { getNetworkSpeed } from "../networkStats";
+import { getNetworkSpeed, simulatePing } from "../networkStats";
 
 
 const { history } = createHistory();
 const { history } = createHistory();
 
 
@@ -291,7 +291,8 @@ class App extends React.Component<ExcalidrawProps, AppState> {
     height: window.innerHeight,
     height: window.innerHeight,
   };
   };
   private scene: Scene;
   private scene: Scene;
-  private netStatsIntervalId?: any;
+  private networkSpeedIntervalId?: any;
+  private pingIntervalId?: any;
   constructor(props: ExcalidrawProps) {
   constructor(props: ExcalidrawProps) {
     super(props);
     super(props);
     const defaultAppState = getDefaultAppState();
     const defaultAppState = getDefaultAppState();
@@ -752,7 +753,7 @@ class App extends React.Component<ExcalidrawProps, AppState> {
     this.removeEventListeners();
     this.removeEventListeners();
     this.scene.destroy();
     this.scene.destroy();
     clearTimeout(touchTimeout);
     clearTimeout(touchTimeout);
-    clearTimeout(this.netStatsIntervalId);
+    clearTimeout(this.networkSpeedIntervalId);
     touchTimeout = 0;
     touchTimeout = 0;
   }
   }
 
 
@@ -897,7 +898,7 @@ class App extends React.Component<ExcalidrawProps, AppState> {
           "change",
           "change",
           this.calculateNetStats,
           this.calculateNetStats,
         );
         );
-        clearTimeout(this.netStatsIntervalId);
+        clearTimeout(this.networkSpeedIntervalId);
       }
       }
     }
     }
 
 
@@ -1026,23 +1027,39 @@ class App extends React.Component<ExcalidrawProps, AppState> {
     }
     }
   }
   }
 
 
-  private calculateNetStats = async () => {
+  private calculateNetStats = () => {
+    this.checkPing();
+    this.checkNetworkSpeed();
+  };
+
+  private checkPing = async () => {
     if (!this.state.showStats || !this.props.isCollaborating) {
     if (!this.state.showStats || !this.props.isCollaborating) {
-      clearTimeout(this.netStatsIntervalId);
+      clearTimeout(this.pingIntervalId);
       return;
       return;
     }
     }
-    const speed = await getNetworkSpeed();
-    const networkSpeed = speed;
+    const ping = await simulatePing();
+    this.setState({ ping });
+    if (this.pingIntervalId) {
+      clearTimeout(this.pingIntervalId);
+    }
+    this.pingIntervalId = setTimeout(this.checkPing, NETWORK_SPEED_TIMEOUT_MS);
+  };
+
+  private checkNetworkSpeed = async () => {
+    if (!this.state.showStats || !this.props.isCollaborating) {
+      clearTimeout(this.networkSpeedIntervalId);
+      return;
+    }
+    const networkSpeed = await getNetworkSpeed();
     this.setState({ networkSpeed });
     this.setState({ networkSpeed });
-    if (this.netStatsIntervalId) {
-      clearTimeout(this.netStatsIntervalId);
+    if (this.networkSpeedIntervalId) {
+      clearTimeout(this.networkSpeedIntervalId);
     }
     }
-    this.netStatsIntervalId = setTimeout(
-      this.calculateNetStats,
+    this.networkSpeedIntervalId = setTimeout(
+      this.checkNetworkSpeed,
       NETWORK_SPEED_TIMEOUT_MS,
       NETWORK_SPEED_TIMEOUT_MS,
     );
     );
   };
   };
-
   // Copy/paste
   // Copy/paste
 
 
   private onCut = withBatchedUpdates((event: ClipboardEvent) => {
   private onCut = withBatchedUpdates((event: ClipboardEvent) => {

+ 4 - 0
src/components/Stats.tsx

@@ -180,6 +180,10 @@ export const Stats = (props: {
                   <td>{t("stats.collaborators")}</td>
                   <td>{t("stats.collaborators")}</td>
                   <td>{props.appState.collaborators.size}</td>
                   <td>{props.appState.collaborators.size}</td>
                 </tr>
                 </tr>
+                <tr>
+                  <td>{t("stats.ping")}</td>
+                  <td>{props.appState.ping}</td>
+                </tr>
                 <tr>
                 <tr>
                   <td>{t("stats.speed")}</td>
                   <td>{t("stats.speed")}</td>
                   <td>
                   <td>

+ 1 - 0
src/locales/en.json

@@ -233,6 +233,7 @@
     "elements": "Elements",
     "elements": "Elements",
     "error": "Error",
     "error": "Error",
     "height": "Height",
     "height": "Height",
+    "ping": "Ping",
     "scene": "Scene",
     "scene": "Scene",
     "selected": "Selected",
     "selected": "Selected",
     "speed": "Speed",
     "speed": "Speed",

+ 15 - 0
src/networkStats.ts

@@ -50,3 +50,18 @@ const processImage = (): Promise<number> => {
 export const getNetworkSpeed = async (): Promise<number> => {
 export const getNetworkSpeed = async (): Promise<number> => {
   return await processImage();
   return await processImage();
 };
 };
+
+export const simulatePing = async () => {
+  const startTime = new Date().getTime();
+  try {
+    await fetch(process.env.REACT_APP_SOCKET_SERVER_URL, {
+      mode: "no-cors",
+      method: "HEAD",
+    });
+    const endTime = new Date().getTime();
+    const delay = endTime - startTime;
+    return delay < 1000 ? `${delay} ms` : `${(delay / 1000).toFixed(1)} s`;
+  } catch (e) {
+    return "Error!";
+  }
+};

+ 1 - 0
src/types.ts

@@ -89,6 +89,7 @@ export type AppState = {
   gridSize: number | null;
   gridSize: number | null;
   viewModeEnabled: boolean;
   viewModeEnabled: boolean;
   networkSpeed: number;
   networkSpeed: number;
+  ping: string;
 
 
   /** top-most selected groups (i.e. does not include nested groups) */
   /** top-most selected groups (i.e. does not include nested groups) */
   selectedGroupIds: { [groupId: string]: boolean };
   selectedGroupIds: { [groupId: string]: boolean };