From 50e43d288e67f2648155123adb8a4757edfda813 Mon Sep 17 00:00:00 2001 From: you21979 Date: Thu, 20 Jul 2017 14:05:16 +0900 Subject: [PATCH] connect error handling --- example/subscribe.js | 28 +++++++++++++++++----------- lib/client.js | 18 ++++++++++++++++++ lib/init_socket.js | 11 +++++++---- package.json | 2 +- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/example/subscribe.js b/example/subscribe.js index dc653bf..5b5fd3b 100644 --- a/example/subscribe.js +++ b/example/subscribe.js @@ -2,17 +2,23 @@ const ElectrumClient = require('..') const sleep = (ms) => new Promise((resolve,_) => setTimeout(() => resolve(), ms)) const main = async () => { - const ecl = new ElectrumClient(995, 'btc.smsys.me', 'tls') - ecl.subscribe.on('server.peers.subscribe', console.log) - ecl.subscribe.on('blockchain.numblocks.subscribe', console.log) - ecl.subscribe.on('blockchain.headers.subscribe', console.log) - await ecl.connect() - const p1 = await ecl.serverPeers_subscribe() - const p2 = await ecl.blockchainHeaders_subscribe() - const p3 = await ecl.blockchainNumblocks_subscribe() - while(true){ - await sleep(1000) + try{ + const ecl = new ElectrumClient(995, 'btc.smsys.me', 'tls') + ecl.subscribe.on('server.peers.subscribe', console.log) + ecl.subscribe.on('blockchain.numblocks.subscribe', console.log) + ecl.subscribe.on('blockchain.headers.subscribe', console.log) + await ecl.connect() + const p1 = await ecl.serverPeers_subscribe() + const p2 = await ecl.blockchainHeaders_subscribe() + const p3 = await ecl.blockchainNumblocks_subscribe() + while(true){ + await sleep(1000) + let version = await ecl.server_version("2.7.11", "1.0") + } + await ecl.close() + }catch(e){ + console.log("error") + console.log(e) } - await ecl.close() } main() diff --git a/lib/client.js b/lib/client.js index 3012feb..6974901 100644 --- a/lib/client.js +++ b/lib/client.js @@ -14,22 +14,37 @@ class Client{ this.mp = new util.MessageParser((body, n) => { this.onMessage(body, n) }) + this.status = 0 } connect(){ + if(this.status) { + return Promise.resolve() + } + this.status = 1 return new Promise((resolve, reject) => { + const errorHandler = (e) => reject(e) this.conn.connect(this.port, this.host, () => { + this.conn.removeListener('error', errorHandler) resolve() }) + this.conn.on('error', errorHandler) }) } close(){ + if(!this.status) { + return + } this.conn.end() this.conn.destroy() + this.status = 0 } request(method, params){ + if(!this.status) { + return Promise.reject(new Error('ESOCKET')) + } return new Promise((resolve, reject) => { const id = ++this.id; const content = util.makeRequest(method, params, id); @@ -82,6 +97,9 @@ class Client{ onEnd(){ } + onError(e){ + } + } module.exports = Client diff --git a/lib/init_socket.js b/lib/init_socket.js index 1bacb28..23e6a91 100644 --- a/lib/init_socket.js +++ b/lib/init_socket.js @@ -21,14 +21,17 @@ const initSocket = (self, protocol, options) => { conn.on('connect', () => { self.onConnect() }) - conn.on('close', () => { - self.onClose() + conn.on('close', (e) => { + self.onClose(e) }) conn.on('data', (chunk) => { self.onRecv(chunk) }) - conn.on('end', () => { - self.onEnd() + conn.on('end', (e) => { + self.onEnd(e) + }) + conn.on('error', (e) => { + self.onError(e) }) return conn } diff --git a/package.json b/package.json index fd170a4..d885459 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "electrum-client", - "version": "0.0.3", + "version": "0.0.4", "description": "Electrum protocol client for node.js", "main": "index.js", "scripts": {