Skip to content

Commit 17dea26

Browse files
author
Adam Rudd
committed
Merge pull request mqttjs#88 from adamvr/fix-end-with-queued
Don't disconnect before connecting
2 parents af05ba4 + 7693eda commit 17dea26

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/client.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,14 +310,22 @@ MqttClient.prototype.unsubscribe = function(topic, callback) {
310310
* @api public
311311
*/
312312
MqttClient.prototype.end = function() {
313+
if (!this.connected) {
314+
this.once('connect', this._cleanUp.bind(this));
315+
} else {
316+
this._cleanUp();
317+
}
318+
319+
return this;
320+
};
321+
322+
MqttClient.prototype._cleanUp = function() {
313323
this.conn.disconnect();
314324
this.stream.end();
315325
if (this.pingTimer !== null) {
316326
clearInterval(this.pingTimer);
317327
this.pingTimer = null;
318328
}
319-
320-
return this;
321329
};
322330

323331
MqttClient.prototype._sendPacket = function(type, packet, cb) {

test/client.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,15 @@ describe('MqttClient', function () {
9494
done();
9595
});
9696

97-
client.end();
97+
client.on('connect', function() {
98+
client.end();
99+
});
100+
101+
this.server.once('client', function(client) {
102+
client.once('connect', function(packet) {
103+
client.connack({returnCode: 0});
104+
});
105+
});
98106
});
99107

100108
it('should stop ping timer after end called', function(done) {

0 commit comments

Comments
 (0)