aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/PingManager.ts
blob: 7562faf03799f4fba86ec8c043551353b4ab220b (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
31
import { action, IReactionDisposer, observable, observe, reaction } 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);

    ping = async (): Promise<void> => {
        try {
            const response = await Networking.PostToServer('/ping', { date: new Date() });
            // console.log('ping response', response, this.interval);
            !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.ping, this.INTERVAL_SECONDS * 1000);
    }
}