Skip to content

Commit 83f3b3d

Browse files
authored
Merge pull request mqttjs#841 from Degola/master
Solves issue mqttjs#810
2 parents cbd1630 + a188757 commit 83f3b3d

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

lib/client.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,11 @@ function MqttClient (streamBuilder, options) {
120120
this.connackTimer = null
121121
// Reconnect timer
122122
this.reconnectTimer = null
123-
// MessageIDs starting with 1
124-
this.nextId = Math.floor(Math.random() * 65535)
123+
/**
124+
* MessageIDs starting with 1
125+
* ensure that nextId is min. 1, see https://github.com/mqttjs/MQTT.js/issues/810
126+
*/
127+
this.nextId = Math.max(1, Math.floor(Math.random() * 65535))
125128

126129
// Inflight callbacks
127130
this.outgoing = {}
@@ -1070,18 +1073,21 @@ MqttClient.prototype._handlePubrel = function (packet, callback) {
10701073

10711074
/**
10721075
* _nextId
1076+
* @return unsigned int
10731077
*/
10741078
MqttClient.prototype._nextId = function () {
1079+
// id becomes current state of this.nextId and increments afterwards
10751080
var id = this.nextId++
1076-
// Ensure 16 bit unsigned int:
1077-
if (id === 65535) {
1081+
// Ensure 16 bit unsigned int (max 65535, nextId got one higher)
1082+
if (this.nextId === 65536) {
10781083
this.nextId = 1
10791084
}
10801085
return id
10811086
}
10821087

10831088
/**
10841089
* getLastMessageId
1090+
* @return unsigned int
10851091
*/
10861092
MqttClient.prototype.getLastMessageId = function () {
10871093
return (this.nextId === 1) ? 65535 : (this.nextId - 1)

0 commit comments

Comments
 (0)