Skip to content

Commit

Permalink
bunch of reconnection fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchg committed Mar 4, 2014
1 parent a8453d9 commit 502ef41
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Socket.IO is compatible with [browserify](http://browserify.org/).
reconnections (`5000`). Each attempt increases the reconnection by
the amount specified by `reconnectionDelay`.
- `timeout` connection timeout before a `connect_error`
and `connect_timeout` events are emitted (`10000`)
and `connect_timeout` events are emitted (`20000`)

#### Events

Expand Down
50 changes: 29 additions & 21 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ module.exports = Manager;
/**
* `Manager` constructor.
*
* @param {Socket|String} engine instance or engine uri/opts
* @param {String} engine instance or engine uri/opts
* @param {Object} options
* @api public
*/

function Manager(socket, opts){
if (!(this instanceof Manager)) return new Manager(socket, opts);
function Manager(uri, opts){
if (!(this instanceof Manager)) return new Manager(uri, opts);
opts = opts || {};
opts.path = opts.path || '/socket.io';
this.nsps = {};
this.subs = [];
this.opts = opts;
this.reconnection(opts.reconnection);
this.reconnectionAttempts(opts.reconnectionAttempts || Infinity);
this.reconnectionDelay(opts.reconnectionDelay || 1000);
this.reconnectionDelayMax(opts.reconnectionDelayMax || 5000);
this.timeout(null == opts.timeout ? 10000 : opts.timeout);
this.timeout(null == opts.timeout ? 20000 : opts.timeout);
this.readyState = 'closed';
if (!socket || !socket.write) socket = eio(socket, opts);
this.engine = socket;
this.uri = uri;
this.connected = 0;
this.attempts = 0;
this.encoding = false;
Expand Down Expand Up @@ -135,12 +135,13 @@ Manager.prototype.timeout = function(v){

Manager.prototype.open =
Manager.prototype.connect = function(fn){
debug('readyState %s', this.readyState);
if (~this.readyState.indexOf('open')) return this;

debug('opening %s', this.uri);
this.engine = eio(this.uri, this.opts);
var socket = this.engine;
var self = this;
var timerSub;

this.readyState = 'opening';

// emit `open`
Expand All @@ -149,6 +150,7 @@ Manager.prototype.connect = function(fn){
// emit `connect_error`
var errorSub = on(socket, 'error', function(data){
self.cleanup();
self.readyState = 'closed';
self.emit('connect_error', data);
if (fn) {
var err = new Error('Connection error');
Expand All @@ -171,14 +173,11 @@ Manager.prototype.connect = function(fn){
self.emit('connect_timeout', timeout);
}, timeout);

// create handle
timerSub = {
this.subs.push({
destroy: function(){
clearTimeout(timer);
}
};

this.subs.push(timerSub);
});
}

this.subs.push(openSub);
Expand All @@ -194,6 +193,8 @@ Manager.prototype.connect = function(fn){
*/

Manager.prototype.onopen = function(){
debug('open');

// clear old subs
this.cleanup();

Expand Down Expand Up @@ -227,7 +228,7 @@ Manager.prototype.ondata = function(data){

Manager.prototype.ondecoded = function(packet) {
this.emit('packet', packet);
}
};

/**
* Called upon socket error.
Expand All @@ -236,6 +237,7 @@ Manager.prototype.ondecoded = function(packet) {
*/

Manager.prototype.onerror = function(err){
debug('error', err);
this.emit('error', err);
};

Expand Down Expand Up @@ -283,7 +285,8 @@ Manager.prototype.packet = function(packet){
debug('writing packet %j', packet);
var self = this;

if (!self.encoding) { // encode, then write to engine with result
if (!self.encoding) {
// encode, then write to engine with result
self.encoding = true;
this.encoder.encode(packet, function(encodedPackets) {
for (var i = 0; i < encodedPackets.length; i++) {
Expand All @@ -309,7 +312,7 @@ Manager.prototype.processPacketQueue = function() {
var pack = this.packetBuffer.shift();
this.packet(pack);
}
}
};

/**
* Clean up transport subscriptions and packet buffer.
Expand All @@ -336,7 +339,6 @@ Manager.prototype.cleanup = function(){
Manager.prototype.close =
Manager.prototype.disconnect = function(){
this.skipReconnect = true;
this.cleanup();
this.engine.close();
};

Expand All @@ -346,10 +348,12 @@ Manager.prototype.disconnect = function(){
* @api private
*/

Manager.prototype.onclose = function(){
Manager.prototype.onclose = function(reason){
debug('close');
this.cleanup();
this.readyState = 'closed';
this.emit('close', reason);
if (!this.skipReconnect) {
var self = this;
this.reconnect();
}
};
Expand All @@ -361,10 +365,13 @@ Manager.prototype.onclose = function(){
*/

Manager.prototype.reconnect = function(){
if (this.reconnecting) return this;

var self = this;
this.attempts++;

if (this.attempts > this._reconnectionAttempts) {
debug('reconnect failed');
this.emit('reconnect_failed');
this.reconnecting = false;
} else {
Expand All @@ -374,12 +381,13 @@ Manager.prototype.reconnect = function(){

this.reconnecting = true;
var timer = setTimeout(function(){
debug('attemptign reconnect');
debug('attempting reconnect');
self.open(function(err){
if (err) {
debug('reconnect attempt error');
self.reconnecting = false;
self.reconnect();
return self.emit('reconnect_error', err.data);
self.emit('reconnect_error', err.data);
} else {
debug('reconnect success');
self.onreconnect();
Expand Down
11 changes: 11 additions & 0 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function Socket(io, nsp){
this.open();
this.buffer = [];
this.connected = false;
this.skipReconnect = false;
this.disconnected = true;
}

Expand All @@ -69,13 +70,15 @@ Emitter(Socket.prototype);

Socket.prototype.open =
Socket.prototype.connect = function(){
if (this.connected) return this;
var io = this.io;
io.open(); // ensure open
this.subs = [
on(io, 'open', bind(this, 'onopen')),
on(io, 'error', bind(this, 'onerror'))
];
if ('open' == this.io.readyState) this.onopen();
return this;
};

/**
Expand Down Expand Up @@ -154,6 +157,11 @@ Socket.prototype.onerror = function(data){
*/

Socket.prototype.onopen = function(){
if (this.io.reconnecting && this.skipReconnect) {
debug('ignoring reconnect');
return;
}

debug('transport is open - connecting');

// write connect packet if necessary
Expand Down Expand Up @@ -314,6 +322,7 @@ Socket.prototype.emitBuffered = function(){
Socket.prototype.ondisconnect = function(){
debug('server disconnect (%s)', this.nsp);
this.destroy();
this.skipReconnect = true;
this.onclose('io server disconnect');
};

Expand Down Expand Up @@ -346,6 +355,8 @@ Socket.prototype.close =
Socket.prototype.disconnect = function(){
if (!this.connected) return this;

this.skipReconnect = true;

debug('performing disconnect (%s)', this.nsp);
this.packet({ type: parser.PACKET_DISCONNECT });

Expand Down

0 comments on commit 502ef41

Please sign in to comment.