Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9dc58fb

Browse files
committed
Connect event provides connack response as first argument.
1 parent 56d90c7 commit 9dc58fb

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,10 @@ version 1.3 and 1.4 works fine without those.
212212

213213
#### Event `'connect'`
214214

215-
`function() {}`
215+
`function(connack) {}`
216216

217-
Emitted on successful (re)connection (i.e. connack rc=0).
217+
Emitted on successful (re)connection (i.e. connack rc=0).
218+
* `connack` received connack packet
218219

219220
#### Event `'reconnect'`
220221

lib/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ MqttClient.prototype._handleConnack = function (packet) {
667667
clearTimeout(this.connackTimer);
668668

669669
if (0 === rc) {
670-
this.emit('connect', null, {sessionPresent: packet.sessionPresent});
670+
this.emit('connect', packet);
671671
} else if (0 < rc) {
672672
this.emit('error',
673673
new Error('Connection refused: ' + errors[rc]));

test/abstract_client.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,26 @@ module.exports = function (server, config) {
199199
client.once('error', done);
200200
});
201201

202+
it('should provide connack packet with connect event', function (done) {
203+
server.once('client', function (serverClient) {
204+
serverClient.connack({returnCode: 0, sessionPresent: true});
205+
206+
server.once('client', function (serverClient) {
207+
serverClient.connack({returnCode: 0, sessionPresent: false});
208+
});
209+
});
210+
211+
var client = connect();
212+
client.once('connect', function (packet) {
213+
should(packet.sessionPresent).be.equal(true);
214+
client.once('connect', function (packet) {
215+
should(packet.sessionPresent).be.equal(false);
216+
client.end();
217+
done();
218+
});
219+
});
220+
});
221+
202222
it('should mark the client as connected', function (done) {
203223
var client = connect();
204224
client.once('connect', function () {

0 commit comments

Comments
 (0)