simulate ping every 5 sec

This commit is contained in:
Aakansha Doshi 2021-02-07 19:25:29 +05:30
parent 163dbd47d4
commit 69a1b74e05
6 changed files with 54 additions and 14 deletions

View File

@ -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">(

View File

@ -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 ping = await simulatePing();
const networkSpeed = speed; this.setState({ ping });
this.setState({ networkSpeed }); if (this.pingIntervalId) {
if (this.netStatsIntervalId) { clearTimeout(this.pingIntervalId);
clearTimeout(this.netStatsIntervalId);
} }
this.netStatsIntervalId = setTimeout( this.pingIntervalId = setTimeout(this.checkPing, NETWORK_SPEED_TIMEOUT_MS);
this.calculateNetStats, };
private checkNetworkSpeed = async () => {
if (!this.state.showStats || !this.props.isCollaborating) {
clearTimeout(this.networkSpeedIntervalId);
return;
}
const networkSpeed = await getNetworkSpeed();
this.setState({ networkSpeed });
if (this.networkSpeedIntervalId) {
clearTimeout(this.networkSpeedIntervalId);
}
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) => {

View File

@ -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>

View File

@ -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",

View File

@ -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!";
}
};

View File

@ -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 };