diff options
author | eperelm2 <emily_perelman@brown.edu> | 2023-07-05 12:48:02 -0400 |
---|---|---|
committer | eperelm2 <emily_perelman@brown.edu> | 2023-07-05 12:48:02 -0400 |
commit | b59241f60140625b80aad5c9455c75fc1f3439ac (patch) | |
tree | 59e3aab7ffced8e6081cdb36eb1cad9465db844c /src/client/util/PingManager.ts | |
parent | 5b7a0804fa2bd4b956b3617501619737814bd28b (diff) | |
parent | 638a3ce3bcd4aa7287544be82d8d9d07ee963600 (diff) |
Merge branch 'master' into secondpropertiesmenu-emily
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); + } +} |