Propagating reconnecting and reconnection events to socket

This commit is contained in:
Tony Kovanen
2014-06-13 03:43:49 +03:00
parent a20671c511
commit 109395ee34
2 changed files with 29 additions and 3 deletions

View File

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

View File

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