diff --git a/lib/manager.js b/lib/manager.js index e92f6efa..ed1e2cca 100644 --- a/lib/manager.js +++ b/lib/manager.js @@ -63,10 +63,10 @@ function Manager(uri, opts){ */ Manager.prototype.propagateReconnectEvents = function() { var self = this; - ['reconnect_attempt', 'reconnect_error', 'reconnect_failed'].forEach(function(ev) { - self.on(ev, function() { + ['reconnect_attempt', 'reconnect_error', 'reconnect_failed', 'reconnect', 'reconnecting'].forEach(function(ev) { + self.on(ev, function(attempts) { for (var nsp in self.nsps) { - self.nsps[nsp].$emit(ev); + self.nsps[nsp].$emit(ev, attempts); } }); }); @@ -424,6 +424,7 @@ Manager.prototype.reconnect = function(){ var timer = setTimeout(function(){ debug('attempting reconnect'); self.emit('reconnect_attempt'); + self.emit('reconnecting', self.attempts); self.open(function(err){ if (err) { debug('reconnect attempt error'); diff --git a/test/connection.js b/test/connection.js index f15a8dff..5673fbdb 100644 --- a/test/connection.js +++ b/test/connection.js @@ -83,6 +83,14 @@ describe('connection', function() { }); }); + it('reconnect event should fire in socket', function(done){ + var socket = io(); + socket.io.engine.close(); + socket.on('reconnect', function() { + done(); + }); + }); + it('should try to reconnect twice and fail when requested two attempts with immediate timeout and reconnect enabled', function(done) { var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 }); var socket; @@ -119,6 +127,23 @@ describe('connection', function() { }); }); + it('should fire reconnecting (on socket) with attempts number when reconnecting twice', function(done) { + var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 }); + var socket = manager.socket('/timeout_socket'); + + var reconnects = 0; + var reconnectCb = function(attempts) { + reconnects++; + expect(attempts).to.be(reconnects); + }; + + socket.on('reconnecting', reconnectCb); + socket.on('reconnect_failed', function failed() { + expect(reconnects).to.be(2); + socket.close(); + done(); + }); + }); it('should not try to reconnect and should form a connection when connecting to correct port with default timeout', function(done) { var manager = io.Manager({ reconnection: true, reconnectionDelay: 10 });