Skip to content

Commit

Permalink
server side disconnect handling (#926)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarry1992 authored and mcollina committed Mar 20, 2019
1 parent 62641d6 commit 524699d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/client.js
Expand Up @@ -351,6 +351,10 @@ MqttClient.prototype._handlePacket = function (packet, done) {
this._handlePingresp(packet)
done()
break
case 'disconnect':
this._handleDisconnect(packet)
done()
break
default:
// do nothing
// maybe we should do an error handling
Expand Down Expand Up @@ -1257,6 +1261,17 @@ MqttClient.prototype._handlePubrel = function (packet, callback) {
})
}

/**
* _handleDisconnect
*
* @param {Object} packet
* @api private
*/
MqttClient.prototype._handleDisconnect = function (packet) {
this.emit('close', packet)
this.end(true)
}

/**
* _nextId
* @return unsigned int
Expand Down
24 changes: 24 additions & 0 deletions test/client.js
Expand Up @@ -880,6 +880,30 @@ describe('MqttClient', function () {
client.subscribe('a/b', {qos: 1})
})
})
it('server side disconnect', function (done) {
this.timeout(15000)
var server327 = new Server(function (client) {
client.on('connect', function (packet) {
client.connack({
reasonCode: 0
})
client.disconnect({reasonCode: 128})
server327.close()
})
})
server327.listen(port + 327)
var opts = {
host: 'localhost',
port: port + 327,
protocolVersion: 5
}

var client = mqtt.connect(opts)
client.once('close', function (disconnectPacket) {
should(disconnectPacket.reasonCode).be.equal(128)
done()
})
})
it('pubrec handling custom reason code', function (done) {
this.timeout(15000)
serverErr.listen(port + 117)
Expand Down

0 comments on commit 524699d

Please sign in to comment.