Skip to content

Commit f6c2d62

Browse files
author
Adam Rudd
committed
Allow specification of a reconnect period
1 parent 9bafa00 commit f6c2d62

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/client.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ var Connection = require('./connection')
1212
var defaultConnectOptions = {
1313
keepalive: 10,
1414
protocolId: 'MQIsdp',
15-
protocolVersion: 3
15+
protocolVersion: 3,
16+
reconnectPeriod: 1000
1617
};
1718

1819
var defaultId = function() {
@@ -159,7 +160,7 @@ function MqttClient(stream, options) {
159160
if (!that.reconnectTimer) {
160161
that.reconnectTimer = setInterval(function () {
161162
that._reconnect();
162-
}, 1000);
163+
}, that.options.reconnectPeriod);
163164
}
164165
});
165166

test/client.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,5 +667,29 @@ describe('MqttClient', function () {
667667
done();
668668
});
669669
});
670+
671+
it('should allow specification of a reconnect period', function(done) {
672+
this.timeout(2200);
673+
var client = createClient(port, {reconnectPeriod: 2000})
674+
, reconnect = false;
675+
676+
var start = process.hrtime()
677+
, end;
678+
679+
client.on('connect', function () {
680+
if (!reconnect) {
681+
client.stream.end();
682+
reconnect = true;
683+
} else {
684+
end = process.hrtime(start);
685+
if (end[0] === 2) {
686+
// Connected in about 2 seconds, that's good enough
687+
done();
688+
} else {
689+
done(new Error('Strange reconnect period'));
690+
}
691+
}
692+
});
693+
});
670694
});
671695
});

0 commit comments

Comments
 (0)