Skip to content

Commit

Permalink
connect error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
you21979 committed Jul 20, 2017
1 parent ecd87fe commit 50e43d2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
28 changes: 17 additions & 11 deletions example/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
18 changes: 18 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -82,6 +97,9 @@ class Client{
onEnd(){
}

onError(e){
}

}

module.exports = Client
11 changes: 7 additions & 4 deletions lib/init_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 50e43d2

Please sign in to comment.