Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

server side disconnect handling #926

Merged
merged 1 commit into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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