From d98e929028d2b05c88a41fe37652737912bead79 Mon Sep 17 00:00:00 2001 From: Dan Janosik Date: Sat, 11 Jul 2020 22:17:53 -0400 Subject: [PATCH] configurable ping period with sensible default (2m instead of 5s) --- index.js | 12 +++++++++--- package.json | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 9a9b93a..056cd73 100644 --- a/index.js +++ b/index.js @@ -15,7 +15,7 @@ class ElectrumClient extends Client { this.timeLastCall = 0; } - initElectrum(electrumConfig, persistencePolicy = { retryPeriod: 10000, maxRetry: 1000, callback: null }) { + initElectrum(electrumConfig, persistencePolicy = { retryPeriod: 10000, maxRetry: 1000, pingPeriod: 120000, callback: null }) { this.persistencePolicy = persistencePolicy; this.electrumConfig = electrumConfig; this.timeLastCall = 0; @@ -110,13 +110,19 @@ class ElectrumClient extends Client { if (this.timeout != null) { clearTimeout(this.timeout); } + + var pingPeriod = 120000; + if (this.persistencePolicy != null && this.persistencePolicy.pingPeriod > 0) { + pingPeriod = this.persistencePolicy.pingPeriod; + } + this.timeout = setTimeout(() => { - if (this.timeLastCall !== 0 && new Date().getTime() > this.timeLastCall + 5000) { + if (this.timeLastCall !== 0 && new Date().getTime() > this.timeLastCall + pingPeriod) { this.server_ping().catch((reason) => { this.log('Keep-Alive ping failed: ', reason); }); } - }, 5000); + }, pingPeriod); } close() { diff --git a/package.json b/package.json index 73be3cb..d6bf75e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "electrum-client", - "version": "1.1.6", + "version": "1.1.7", "description": "Electrum protocol client for React Native & Node.js", "main": "index.js", "scripts": {