configurable ping period with sensible default (2m instead of 5s)

This commit is contained in:
Dan Janosik 2020-07-11 22:17:53 -04:00
parent 344ac93946
commit d98e929028
No known key found for this signature in database
GPG Key ID: C6F8CE9FFDB2CED2
2 changed files with 10 additions and 4 deletions

View File

@ -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() {

View File

@ -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": {