Skip to content

Commit

Permalink
send ping msg periodically to avoid disconnects
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesGDiaz committed Feb 20, 2022
1 parent 50ec03d commit 7116168
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/exchanges/DigifinexClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/unbound-method */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { setInterval } from "timers";
import { BasicClient } from "../BasicClient";
import { ClientOptions } from "../ClientOptions";
import { Level2Point } from "../Level2Point";
Expand All @@ -19,6 +20,7 @@ import * as zlib from "../ZlibUtils";
*/
export class DigifinexClient extends BasicClient {
public id: number;
private _pingInterval: NodeJS.Timeout;

constructor({ wssPath = "wss://openapi.digifinex.com/ws/v1/", watcherMs }: ClientOptions = {}) {
super(wssPath, "Digifinex", undefined, watcherMs);
Expand All @@ -29,6 +31,33 @@ export class DigifinexClient extends BasicClient {
this._onMessageInf = this._onMessageInf.bind(this);
}

protected _beforeConnect() {
this._wss.on("connected", this._startPing.bind(this));
this._wss.on("disconnected", this._stopPing.bind(this));
this._wss.on("closed", this._stopPing.bind(this));
}

protected _startPing() {
clearInterval(this._pingInterval);
this._pingInterval = setInterval(this._sendPing.bind(this), 15000);
}

protected _stopPing() {
clearInterval(this._pingInterval);
}

protected _sendPing() {
if (this._wss) {
this._wss.send(
JSON.stringify({
"id": Math.random() * (9999999999 - 1000000000) + 1000000000,
"method": "server.ping",
"params": []
})
)
}
}

protected _sendSubTicker(remote_id) {
this._wss.send(
JSON.stringify({
Expand Down

0 comments on commit 7116168

Please sign in to comment.