Skip to content
This repository was archived by the owner on Sep 9, 2023. It is now read-only.

send ping msg periodically to avoid disconnects #317

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 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 @@ -27,6 +29,34 @@ export class DigifinexClient extends BasicClient {
this.hasLevel2Updates = true;
this.id = 0;
this._onMessageInf = this._onMessageInf.bind(this);
this._sendPing = this._sendPing.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, 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) {
Expand Down