[FIX] server: fixed reconnect logic bugs

This commit is contained in:
Matthew Fettig
2014-07-19 22:23:36 -07:00
committed by Geert Weening
parent 5ac21f993e
commit ca88298b76
2 changed files with 17 additions and 5 deletions

View File

@@ -190,6 +190,7 @@ Server.prototype._setState = function(state) {
switch (state) {
case 'online':
this._connected = true;
this._retry = 0;
this.emit('connect');
break;
case 'offline':
@@ -310,6 +311,12 @@ Server.prototype.disconnect = function() {
return;
}
//these need to be reset so that updateScore
//and checkActivity do not trigger reconnect
this._lastLedgerIndex = NaN;
this._lastLedgerClose = NaN;
this._score = 0;
this._shouldConnect = false;
this._setState('offline');
@@ -327,14 +334,19 @@ Server.prototype.disconnect = function() {
Server.prototype.reconnect = function() {
var self = this;
function disconnected() {
function reconnect() {
self._shouldConnect = true;
self._retry = 0;
self.connect();
};
if (this._ws) {
this.once('disconnect', disconnected);
this.disconnect();
if (this._ws && this._shouldConnect) {
if (this._connected) {
this.once('disconnect', reconnect);
this.disconnect();
} else {
reconnect();
}
}
};

View File

@@ -481,7 +481,7 @@ describe('Server', function() {
it('Reconnect', function(done) {
var server = new Server(new Remote(), 'ws://localhost:5748');
server._connected = true;
server._shouldConnect = true;
server._ws = { };
var disconnected = false;