bitpay / bitcore-p2p

Interface to the bitcoin P2P network for bitcore
MIT License
80 stars 276 forks source link

socket is closed #64

Open fanatid opened 9 years ago

fanatid commented 9 years ago

Check connection status before write? https://github.com/bitpay/bitcore-p2p/blob/master/lib/peer.js#L194

var bitcore = require('bitcore')
var Peer = require('bitcore-p2p').Peer
var peer = new Peer({host: 'localhost', port: 18333, network: bitcore.Networks.testnet})
peer.on('connect', function () { peer.disconnect() })
peer.connect()

Result:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: This socket is closed.
    at Socket._write (net.js:638:19)
    at doWrite (_stream_writable.js:226:10)
    at writeOrBuffer (_stream_writable.js:216:5)
    at Socket.Writable.write (_stream_writable.js:183:11)
    at Socket.write (net.js:616:40)
    at Peer.sendMessage (/home/kirill/.../node_modules/bitcore-p2p/lib/peer.js:194:15)
    at Peer._sendVersion (/home/kirill/.../node_modules/bitcore-p2p/lib/peer.js:204:8)
    at Socket.<anonymous> (/home/kirill/.../node_modules/bitcore-p2p/lib/peer.js:146:10)
    at Socket.emit (events.js:117:20)
    at Object.afterConnect [as oncomplete] (net.js:887:10)
braydonf commented 9 years ago

If there was a check, it would still throw an error though. It could be better to check the status before sending a message and then if it's not ready, use a different peer. Alternatively peers in a pool are removed when they are disconnected, and pool.sendMessage will send to all the connected peers.

fanatid commented 9 years ago

I think custom error is better than This socket is closed because node.js process will be alive. Thanks for hint about connected peers in Pool, I'll see.