Skip to content

Commit e85de21

Browse files
committed
Merge pull request mqttjs#387 from gkwicker/patch-2
Introduce 'fastClientDisconnectDetect' option
2 parents 964c8ce + 1157ea9 commit e85de21

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ The arguments are:
174174
the `connect` event. Typically a `net.Socket`.
175175
* `options` is the client connection options (see: the [connect packet](https://github.com/mcollina/mqtt-packet#connect)). Defaults:
176176
* `keepalive`: `10` seconds, set to `0` to disable
177+
* `reschedulePings`: reschedule ping messages after sending packets (default `true`)
177178
* `clientId`: `'mqttjs_' + Math.random().toString(16).substr(2, 8)`
178179
* `protocolId`: `'MQTT'`
179180
* `protocolVersion`: `4`

lib/client.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var events = require('events'),
1616
},
1717
defaultConnectOptions = {
1818
keepalive: 10,
19+
reschedulePings: true,
1920
protocolId: 'MQTT',
2021
protocolVersion: 4,
2122
reconnectPeriod: 1000,
@@ -642,7 +643,7 @@ MqttClient.prototype._setupPingTimer = function () {
642643
* @api private
643644
*/
644645
MqttClient.prototype._shiftPingInterval = function () {
645-
if (this.pingTimer && this.options.keepalive) {
646+
if (this.pingTimer && this.options.keepalive && this.options.reschedulePings) {
646647
this.pingTimer.reschedule(this.options.keepalive * 1000);
647648
}
648649
};

test/abstract_client.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,38 @@ module.exports = function (server, config) {
605605
done();
606606
});
607607
});
608+
it('should not checkPing if publishing at a higher rate than keepalive', function (done) {
609+
var intervalMs = 3000,
610+
client = connect({keepalive: intervalMs / 1000});
611+
612+
client._checkPing = sinon.spy();
613+
614+
client.once('connect', function () {
615+
client.publish('foo', 'bar');
616+
clock.tick(intervalMs - 1);
617+
client.publish('foo', 'bar');
618+
clock.tick(2);
619+
client._checkPing.callCount.should.equal(0);
620+
client.end();
621+
done();
622+
});
623+
});
624+
it('should checkPing if publishing at a higher rate than keepalive and reschedulePings===false', function (done) {
625+
var intervalMs = 3000,
626+
client = connect({keepalive: intervalMs / 1000,reschedulePings:false});
627+
628+
client._checkPing = sinon.spy();
629+
630+
client.once('connect', function () {
631+
client.publish('foo', 'bar');
632+
clock.tick(intervalMs - 1);
633+
client.publish('foo', 'bar');
634+
clock.tick(2);
635+
client._checkPing.callCount.should.equal(1);
636+
client.end();
637+
done();
638+
});
639+
});
608640
});
609641

610642
describe('pinging', function () {

0 commit comments

Comments
 (0)