diff --git a/js/controllers/join.js b/js/controllers/join.js index 3cf0033ce..fcf9fd8d4 100644 --- a/js/controllers/join.js +++ b/js/controllers/join.js @@ -120,7 +120,7 @@ angular.module('copayApp.controllers').controller('JoinController', $scope.loading = false; if (err || !w) { if (err === 'joinError') - notification.error('Can\'t find peer.'); + notification.error('Fatal error connecting to Insight server'); else if (err === 'walletFull') notification.error('The wallet is full'); else if (err === 'badNetwork') diff --git a/js/models/core/WalletFactory.js b/js/models/core/WalletFactory.js index 3b4d9d77e..61a384f75 100644 --- a/js/models/core/WalletFactory.js +++ b/js/models/core/WalletFactory.js @@ -207,7 +207,6 @@ WalletFactory.prototype.getWallets = function() { WalletFactory.prototype.delete = function(walletId, cb) { var s = this.storage; - this.log('## DELETING WALLET ID:' + walletId); //TODO s.deleteWallet(walletId); s.setLastOpened(undefined); return cb(); @@ -251,13 +250,12 @@ WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphras connectedOnce = true; }); - self.network.on('serverError', function() { + self.network.on('serverError', function() { return cb('joinError'); }); self.network.start(opts, function() { self.network.greet(s.pubKey); - self.network.on('data', function(sender, data) { if (data.type === 'walletId') { if (data.networkName !== self.networkName) { diff --git a/js/models/network/Async.js b/js/models/network/Async.js index eb53a3c83..cb7d078ee 100644 --- a/js/models/network/Async.js +++ b/js/models/network/Async.js @@ -72,6 +72,7 @@ Network.prototype.connectedCopayers = function() { }; Network.prototype._sendHello = function(copayerId) { + this.send(copayerId, { type: 'hello', copayerId: this.copayerId, @@ -211,13 +212,6 @@ Network.prototype._onMessage = function(enc) { Network.prototype._setupConnectionHandlers = function(cb) { preconditions.checkState(this.socket); var self = this; - - self.socket.on('connect', function() { - self.socket.on('disconnect', function() { - self.cleanUp(); - }); - if (typeof cb === 'function') cb(); - }); self.socket.on('message', function(m) { // delay execution, to improve error handling setTimeout(function() { @@ -226,6 +220,14 @@ Network.prototype._setupConnectionHandlers = function(cb) { }); self.socket.on('error', self._onError.bind(self)); + self.socket.on('connect', function() { + + self.socket.on('disconnect', function() { + self.cleanUp(); + }); + + if (typeof cb === 'function') cb(); + }); }; Network.prototype._onError = function(err) { @@ -285,9 +287,23 @@ Network.prototype.start = function(opts, openCallback) { this.socket = this.createSocket(); this._setupConnectionHandlers(openCallback); this.socket.emit('subscribe', pubkey); - this.socket.emit('sync', opts.lastTimestamp); - this.started = true; + var self = this, + tries = 0; + self.socket.on('insight-error', function(m) { + + console.log('Retrying to sync...'); + setTimeout(function() { + if (tries++ > 5) { + self.emit('serverError'); + } else { + self.socket.emit('sync', opts.lastTimestamp); + } + }, 500); + }); + + self.socket.emit('sync', opts.lastTimestamp); + self.started = true; }; Network.prototype.createSocket = function() { @@ -329,7 +345,6 @@ Network.prototype.send = function(dest, payload, cb) { dest = this.getCopayerIds(); payload.isBroadcast = 1; } - if (typeof dest === 'string') dest = [dest]; @@ -339,6 +354,7 @@ Network.prototype.send = function(dest, payload, cb) { dest.forEach(function(to) { //console.log('\t to ' + to); var message = self.encode(to, payload); + self.socket.emit('message', message); }); if (typeof cb === 'function') cb();