From cc018effafd2256272f348953500c4d27cef0d2f Mon Sep 17 00:00:00 2001 From: Overtorment Date: Fri, 12 Jun 2020 15:47:10 +0100 Subject: [PATCH] REF: reconnect improvements; remove unused code --- index.js | 7 +++++-- lib/TlsSocketWrapper.js | 12 ------------ lib/client.js | 24 +++--------------------- package.json | 2 +- 4 files changed, 9 insertions(+), 36 deletions(-) diff --git a/index.js b/index.js index 266564a..faf4769 100644 --- a/index.js +++ b/index.js @@ -52,7 +52,7 @@ class ElectrumClient extends Client { } else if (this.persistencePolicy == null) { this.reconnect(); } - }, 10000); + }, 1000); } // ElectrumX persistancy @@ -62,7 +62,9 @@ class ElectrumClient extends Client { } this.timeout = setTimeout(() => { if (this.timeLastCall !== 0 && new Date().getTime() > this.timeLastCall + 5000) { - this.server_ping(); + this.server_ping().catch((reason) => { + console.log('keepalive ping failed because of', reason); + }); } }, 5000); } @@ -76,6 +78,7 @@ class ElectrumClient extends Client { } reconnect() { + console.log('electrum reconnect'); this.initSocket(); return this.initElectrum(this.electrumConfig); } diff --git a/lib/TlsSocketWrapper.js b/lib/TlsSocketWrapper.js index 849a38c..ca29048 100644 --- a/lib/TlsSocketWrapper.js +++ b/lib/TlsSocketWrapper.js @@ -85,15 +85,6 @@ class TlsSocketWrapper { this._socket.on('data', data => { this._passOnEvent('data', data); }); - this._socket.on('end', data => { - this._passOnEvent('end', data); - }); - this._socket.on('timeout', () => { - this._passOnEvent('timeout'); - }); - this._socket.on('onerror', data => { - this._passOnEvent('onerror', data); - }); this._socket.on('error', data => { this._passOnEvent('error', data); }); @@ -103,9 +94,6 @@ class TlsSocketWrapper { this._socket.on('connect', data => { this._passOnEvent('connect', data); }); - this._socket.on('secureConnect', data => { - this._passOnEvent('secureConnect', data); - }); this._socket.on('connection', data => { this._passOnEvent('connection', data); }); diff --git a/lib/client.js b/lib/client.js index 73d0857..6fdf6b5 100644 --- a/lib/client.js +++ b/lib/client.js @@ -61,27 +61,13 @@ class Client { this.conn.on('close', e => { this.onClose(e); }); - this.conn.on('timeout', () => { - const e = new Error('ETIMEDOUT'); - e.errorno = 'ETIMEDOUT'; - e.code = 'ETIMEDOUT'; - e.connect = false; - this.conn.emit('error', e); - }); this.conn.on('data', chunk => { this.conn.setTimeout(0); this.onRecv(chunk); }); - this.conn.on('end', e => { - this.conn.setTimeout(0); - this.onEnd(e); - }); this.conn.on('error', e => { this.onError(e); }); - this.conn.on('onerror', e => { - this.onError(e); - }); this.status = 0; } @@ -115,7 +101,7 @@ class Client { request(method, params) { if (this.status === 0) { - return Promise.reject(new Error('ESOCKET')); + return Promise.reject(new Error('Connection to server lost, please retry')); } return new Promise((resolve, reject) => { const id = ++this.id; @@ -127,7 +113,7 @@ class Client { requestBatch(method, params, secondParam) { if (this.status === 0) { - return Promise.reject(new Error('ESOCKET')); + return Promise.reject(new Error('Connection to server lost, please retry')); } return new Promise((resolve, reject) => { let arguments_far_calls = {}; @@ -170,7 +156,7 @@ class Client { callback(null, msg.result || msg); } } else { - console.log("Can't get callback"); // can't get callback + console.log("Can't get callback"); } } @@ -201,10 +187,6 @@ class Client { this.mp.run(chunk); } - onEnd(e) { - console.log('OnEnd:' + e); - } - onError(e) { console.log('OnError:' + e); } diff --git a/package.json b/package.json index 9ae4b98..6ff5692 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "electrum-client", - "version": "1.1.2", + "version": "1.1.3", "description": "Electrum protocol client for React Native & Node.js", "main": "index.js", "scripts": {