aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/PingManager.ts
blob: 9f4bd3642412e2e50f89820877e7ec07964ad3a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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: boolean = true;
    private setIsBeating = action((status: boolean) => (this.IsBeating = status));

    sendPing = async (): Promise<void> => {
        try {
            await Networking.PostToServer('/ping', { date: new Date() });
            !this.IsBeating && this.setIsBeating(true);
        } catch {
            console.error('ping error');
            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);
    }
}