diff options
author | Sophie Zhang <sophie_zhang@brown.edu> | 2023-07-18 15:06:22 -0400 |
---|---|---|
committer | Sophie Zhang <sophie_zhang@brown.edu> | 2023-07-18 15:06:22 -0400 |
commit | 40784b7265851b27e043c07e5f9038a0b29af8b7 (patch) | |
tree | 1f9e154913820cc1cb5952a9d444d9a0eca86c29 /src/client/util/PingManager.ts | |
parent | 162ca319eae256be523f2ee75e7aae7a9a408e37 (diff) | |
parent | 267f5d7c6a87b955c2fa2121c6db7e01cfc1c148 (diff) |
Merge branch 'master' into sophie-ai-images
Diffstat (limited to 'src/client/util/PingManager.ts')
-rw-r--r-- | src/client/util/PingManager.ts | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/client/util/PingManager.ts b/src/client/util/PingManager.ts new file mode 100644 index 000000000..0c41a1ea7 --- /dev/null +++ b/src/client/util/PingManager.ts @@ -0,0 +1,37 @@ +import { action, observable } from 'mobx'; +import { Networking } from '../Network'; +export class PingManager { + // create static instance and getter for global use + @observable static _instance: PingManager; + static get Instance(): PingManager { + return PingManager._instance; + } + + @observable IsBeating = true; + private setIsBeating = action((status: boolean) => { + this.IsBeating = status; + setTimeout(this.showAlert, 100); + }); + + showAlert = () => { + alert(PingManager.Instance.IsBeating ? 'The server connection is active' : 'The server connection has been interrupted.NOTE: Any changes made will appear to persist but will be lost after a browser refreshes.'); + }; + sendPing = async (): Promise<void> => { + try { + await Networking.PostToServer('/ping', { date: new Date() }); + !this.IsBeating && this.setIsBeating(true); + } catch { + if (this.IsBeating) { + this.setIsBeating(false); + } + } + }; + + // not used now, but may need to clear interval + private _interval: NodeJS.Timeout | null = null; + INTERVAL_SECONDS = 1; + constructor() { + PingManager._instance = this; + this._interval = setInterval(this.sendPing, this.INTERVAL_SECONDS * 1000); + } +} |