aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/PingManager.ts
diff options
context:
space:
mode:
authorJames Hu <51237606+jameshu111@users.noreply.github.com>2023-07-06 11:21:48 -0400
committerJames Hu <51237606+jameshu111@users.noreply.github.com>2023-07-06 11:21:48 -0400
commit5379662bdc9279e43d35166ce8bafe042d0d7fb6 (patch)
tree53d474435e772d0652b7ac321224b21c89dbcaad /src/client/util/PingManager.ts
parentabeda176df59157f3194dd86966725f6193997c6 (diff)
parente4ad92e7300ee7844a514379c8a01d0f01cb3a59 (diff)
Merge branch 'master' into james-azure-image
Diffstat (limited to 'src/client/util/PingManager.ts')
-rw-r--r--src/client/util/PingManager.ts37
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);
+ }
+}