File tree Expand file tree Collapse file tree 1 file changed +10
-4
lines changed Expand file tree Collapse file tree 1 file changed +10
-4
lines changed Original file line number Diff line number Diff line change @@ -120,8 +120,11 @@ function MqttClient (streamBuilder, options) {
120
120
this . connackTimer = null
121
121
// Reconnect timer
122
122
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 ) )
125
128
126
129
// Inflight callbacks
127
130
this . outgoing = { }
@@ -1070,18 +1073,21 @@ MqttClient.prototype._handlePubrel = function (packet, callback) {
1070
1073
1071
1074
/**
1072
1075
* _nextId
1076
+ * @return unsigned int
1073
1077
*/
1074
1078
MqttClient . prototype . _nextId = function ( ) {
1079
+ // id becomes current state of this.nextId and increments afterwards
1075
1080
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 ) {
1078
1083
this . nextId = 1
1079
1084
}
1080
1085
return id
1081
1086
}
1082
1087
1083
1088
/**
1084
1089
* getLastMessageId
1090
+ * @return unsigned int
1085
1091
*/
1086
1092
MqttClient . prototype . getLastMessageId = function ( ) {
1087
1093
return ( this . nextId === 1 ) ? 65535 : ( this . nextId - 1 )
You can’t perform that action at this time.
0 commit comments