Merge pull request #1214 from matiu/bug/join-wallet

Bug/join wallet
This commit is contained in:
Gustavo Maximiliano Cortez 2014-08-28 10:14:45 -03:00
commit 6af571547e
3 changed files with 28 additions and 14 deletions

View File

@ -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')

View File

@ -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) {

View File

@ -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();