Skip to content

Commit

Permalink
fix (#917)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarry1992 authored and mcollina committed Feb 14, 2019
1 parent 034f855 commit 62641d6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/client.js
Expand Up @@ -1283,9 +1283,9 @@ MqttClient.prototype.getLastMessageId = function () {
* _resubscribe
* @api private
*/
MqttClient.prototype._resubscribe = function () {
MqttClient.prototype._resubscribe = function (connack) {
if (!this._firstConnection &&
this.options.clean &&
(this.options.clean || (this.options.protocolVersion === 5 && !connack.sessionPresent)) &&
Object.keys(this._resubscribeTopics).length > 0) {
if (this.options.resubscribe) {
this._resubscribeTopics.resubscribe = true
Expand All @@ -1312,7 +1312,7 @@ MqttClient.prototype._onConnect = function (packet) {
var that = this

this._setupPingTimer()
this._resubscribe()
this._resubscribe(packet)

this.connected = true

Expand Down
45 changes: 45 additions & 0 deletions test/client.js
Expand Up @@ -706,6 +706,51 @@ describe('MqttClient', function () {
done()
})
})

it('should resubscribe when reconnecting with protocolVersion 5 and Session Present flag is false', function (done) {
this.timeout(15000)
var tryReconnect = true
var reconnectEvent = false
var server316 = new Server(function (client) {
client.on('connect', function (packet) {
client.connack({
reasonCode: 0,
sessionPresent: false
})
client.on('subscribe', function () {
if (!tryReconnect) {
client.end()
server316.close()
done()
}
})
})
}).listen(port + 316)
var opts = {
host: 'localhost',
port: port + 316,
protocolVersion: 5
}
var client = mqtt.connect(opts)

client.on('reconnect', function () {
reconnectEvent = true
})

client.on('connect', function (connack) {
should(connack.sessionPresent).be.equal(false)
if (tryReconnect) {
client.subscribe('hello', function () {
client.stream.end()
})

tryReconnect = false
} else {
reconnectEvent.should.equal(true)
}
})
})

var serverErr = new Server(function (client) {
client.on('connect', function (packet) {
client.connack({
Expand Down

0 comments on commit 62641d6

Please sign in to comment.