fix reconnect to insight

This commit is contained in:
Matias Alejo Garcia 2014-09-09 20:55:51 -03:00
commit 45fd46e3bc
5 changed files with 77 additions and 53 deletions

View file

@ -4,7 +4,7 @@ var defaultConfig = {
// DEFAULT network (livenet or testnet) // DEFAULT network (livenet or testnet)
networkName: 'testnet', networkName: 'testnet',
forceNetwork: false, forceNetwork: false,
logLevel: 'info', logLevel: 'debug',
// DEFAULT unit: Bit // DEFAULT unit: Bit
unitName: 'bits', unitName: 'bits',

View file

@ -1,6 +1,7 @@
var config = require('../config'); var config = require('../config');
var _ = require('underscore'); var _ = require('underscore');
console.log('[log.js.3]'); //TODO
/** /**
* @desc * @desc
* A simple logger that wraps the <tt>console.log</tt> methods when available. * A simple logger that wraps the <tt>console.log</tt> methods when available.
@ -100,6 +101,9 @@ Logger.prototype.setLevel = function(level) {
*/ */
var logger = new Logger('copay'); var logger = new Logger('copay');
console.log('Log level:' + config.logLevel);
logger.setLevel(config.logLevel); logger.setLevel(config.logLevel);
module.exports = logger; module.exports = logger;

View file

@ -5,6 +5,7 @@ var async = require('async');
var request = require('request'); var request = require('request');
var bitcore = require('bitcore'); var bitcore = require('bitcore');
var io = require('socket.io-client'); var io = require('socket.io-client');
var log = require('../../log');
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
var preconditions = require('preconditions').singleton(); var preconditions = require('preconditions').singleton();
@ -72,12 +73,8 @@ Insight.prototype._getSocketIO = function(url, opts) {
return io(this.url, this.opts); return io(this.url, this.opts);
}; };
/** @private */
Insight.prototype.getSocket = function(url, opts) {
if (!this.socket) {
this.socket = this._getSocketIO(this.url, this.opts);
Insight.prototype._setMainHandlers = function(url, opts) {
// Emmit connection events // Emmit connection events
var self = this; var self = this;
this.socket.on('connect', function() { this.socket.on('connect', function() {
@ -100,9 +97,19 @@ Insight.prototype.getSocket = function(url, opts) {
this.socket.on('reconnect', function(attempt) { this.socket.on('reconnect', function(attempt) {
if (self.status != self.STATUS.DISCONNECTED) return; if (self.status != self.STATUS.DISCONNECTED) return;
self.emit('reconnect', attempt);
self.reSubscribe();
self.status = self.STATUS.CONNECTED; self.status = self.STATUS.CONNECTED;
self.emit('connect', attempt);
}); });
};
/** @private */
Insight.prototype.getSocket = function(url, opts) {
if (!this.socket) {
this.socket = this._getSocketIO(this.url, this.opts);
this._setMainHandlers();
} }
return this.socket; return this.socket;
} }
@ -124,8 +131,6 @@ Insight.prototype.requestPost = function(path, data, cb) {
} }
Insight.prototype.destroy = function() { Insight.prototype.destroy = function() {
console.log('[Insight.js.127] INSIGHT destroy' ); //TODO
var socket = this.getSocket(); var socket = this.getSocket();
this.socket.disconnect(); this.socket.disconnect();
this.socket.removeAllListeners(); this.socket.removeAllListeners();
@ -143,6 +148,9 @@ Insight.prototype.subscribe = function(addresses) {
return function(txid) { return function(txid) {
// verify the address is still subscribed // verify the address is still subscribed
if (!self.subscribed[address]) return; if (!self.subscribed[address]) return;
log.debug('insight tx event');
self.emit('tx', { self.emit('tx', {
address: address, address: address,
txid: txid txid: txid
@ -150,14 +158,18 @@ Insight.prototype.subscribe = function(addresses) {
} }
} }
var s = self.getSocket();
addresses.forEach(function(address) { addresses.forEach(function(address) {
preconditions.checkArgument(new bitcore.Address(address).isValid()); preconditions.checkArgument(new bitcore.Address(address).isValid());
// skip already subscibed // skip already subscibed
if (!self.subscribed[address]) { if (!self.subscribed[address]) {
self.subscribed[address] = true; var handler = handlerFor(self, address);
self.getSocket().emit('subscribe', address); self.subscribed[address] = handler;
self.getSocket().on(address, handlerFor(self, address)); log.debug('Subcribe to: ', address);
s.emit('subscribe', address);
s.on(address, handler);
} }
}); });
}; };
@ -166,20 +178,20 @@ Insight.prototype.getSubscriptions = function(addresses) {
return this.subscribed; return this.subscribed;
} }
Insight.prototype.unsubscribe = function(addresses) {
addresses = Array.isArray(addresses) ? addresses : [addresses];
var self = this;
addresses.forEach(function(address) { Insight.prototype.reSubscribe = function() {
preconditions.checkArgument(new bitcore.Address(address).isValid()); log.debug('insight reSubscribe');
self.getSocket().removeEventListener(address); var allAddresses = Object.keys(this.subscribed);
delete self.subscribed[address]; this.subscribed = {};
}); var s = this.socket;
if (s) {
s.removeAllListeners();
this._setMainHandlers();
this.subscribe(allAddresses);
this.subscribeToBlocks();
}
}; };
Insight.prototype.unsubscribeAll = function() {
this.unsubscribe(Object.keys(this.subscribed));
};
Insight.prototype.broadcast = function(rawtx, cb) { Insight.prototype.broadcast = function(rawtx, cb) {
preconditions.checkArgument(rawtx); preconditions.checkArgument(rawtx);

View file

@ -649,10 +649,19 @@ Wallet.prototype._lockIncomming = function() {
Wallet.prototype._setBlockchainListeners = function() { Wallet.prototype._setBlockchainListeners = function() {
var self = this; var self = this;
this.blockchain.on('connect', self.emit.bind(self,'networkReconnected')); this.blockchain.removeAllListeners();
this.blockchain.on('disconnect', self.emit.bind(self,'networkError'));
this.blockchain.on('reconnect', function(attempts) {
log.debug('blockchain reconnect event');
self.emit('insightReconnected');
});
this.blockchain.on('disconnect', function() {
log.debug('blockchain disconnect event');
self.emit('insightError');
});
this.blockchain.on('tx', function(tx) { this.blockchain.on('tx', function(tx) {
log.debug('blockchain tx event');
self.emit('tx', tx.address); self.emit('tx', tx.address);
}); });
@ -661,8 +670,6 @@ Wallet.prototype._setBlockchainListeners = function() {
} }
} }
/** /**
* @desc Sets up the networking with other peers. * @desc Sets up the networking with other peers.
* *

View file

@ -83,14 +83,15 @@ angular.module('copayApp.services')
}); });
}); });
w.on('networkReconnected', function() { w.on('insightReconnected', function() {
$rootScope.reconnecting = false; $rootScope.reconnecting = false;
root.updateAddressList();
root.updateBalance(function() { root.updateBalance(function() {
$rootScope.$digest(); $rootScope.$digest();
}); });
}); });
w.on('networkError', function() { w.on('insightError', function() {
$rootScope.reconnecting = true; $rootScope.reconnecting = true;
$rootScope.$digest(); $rootScope.$digest();
}); });