bitpay / bitcore-p2p

Interface to the bitcoin P2P network for bitcore
MIT License
81 stars 275 forks source link

Event “disconnect” is always emitted when sending message to a testnet node #96

Closed HungNguyen007 closed 7 years ago

HungNguyen007 commented 7 years ago

Hello ,

When I tried to send message to a testnet node, an event “disconnect” will be always emitted. But when I tried with mainnet, this issues does not appear. Is there any additional condition for testnet?

My snippet likes below.


    var bitcore = require(‘bitcore-lib’);
    var Peer = require(‘bitcore-p2p’).Peer;
    var Networks = bitcore.Networks;
    var p2p = require(‘bitcore-p2p’)
    var Messages = p2p.Messages;

    var peer = new Peer({host: ‘104.237.131.138’, port: 18333, network: Networks.testnet})

    peer.on(‘ready’, function() {
    // peer info
    console.log(peer.version, peer.subversion, peer.bestHeight);
    var messages = new Messages();
    var message = messages.Ping();
    // var message = messages.GetAddr()
    peer.sendMessage(message);
    });

    peer.on(‘connect’, function() {
    // peer info
    console.log(“Node is connecting”);
    });

    peer.on(‘connected’, function() {
    // peer info
    console.log(“Node is connected”);
    });

    peer.on(‘disconnect’, function() {
    console.log(‘connection closed’);
    });

    peer.on(‘addr’, function(message) {
    // message.addresses[]
    message.addresses.forEach(function(address) {
    // do something
    console.log('Address is ’ + JSON.stringify(address))
    });
    });

    peer.connect();
HungNguyen007 commented 7 years ago

I have found out the reason. The instance of Messages must be declared with network is testnet as example below. var messages = new Messages({network: Networks.testnet});