mqtt 4.0.0 → 4.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -24
- package/bin/mqtt.js +27 -0
- package/bin/pub.js +0 -0
- package/bin/sub.js +0 -0
- package/dist/mqtt.js +2794 -2512
- package/dist/mqtt.min.js +1 -1
- package/lib/client.js +72 -59
- package/lib/connect/index.js +7 -2
- package/lib/connect/tcp.js +2 -0
- package/lib/connect/tls.js +3 -0
- package/lib/connect/ws.js +194 -34
- package/lib/connect/wx.js +3 -3
- package/lib/store.js +2 -2
- package/mqtt.js +0 -24
- package/package.json +9 -10
- package/types/lib/client-options.d.ts +14 -1
- package/types/lib/client.d.ts +4 -3
- package/examples/client/secure-client.js +0 -24
- package/examples/client/simple-both.js +0 -13
- package/examples/client/simple-publish.js +0 -7
- package/examples/client/simple-subscribe.js +0 -9
- package/examples/tls client/crt.ca.cg.pem +0 -34
- package/examples/tls client/mqttclient.js +0 -48
- package/examples/tls client/tls-cert.pem +0 -13
- package/examples/tls client/tls-key.pem +0 -15
- package/examples/wss/client.js +0 -49
- package/examples/wss/client_with_proxy.js +0 -58
- package/test/abstract_client.js +0 -3139
- package/test/abstract_store.js +0 -135
- package/test/browser/server.js +0 -132
- package/test/browser/test.js +0 -92
- package/test/client.js +0 -540
- package/test/client_mqtt5.js +0 -537
- package/test/helpers/port_list.js +0 -45
- package/test/helpers/private-csr.pem +0 -16
- package/test/helpers/private-key.pem +0 -27
- package/test/helpers/public-cert.pem +0 -19
- package/test/helpers/public-key.pem +0 -9
- package/test/helpers/server.js +0 -53
- package/test/helpers/server_process.js +0 -9
- package/test/helpers/tls-cert.pem +0 -14
- package/test/helpers/tls-csr.pem +0 -11
- package/test/helpers/tls-key.pem +0 -15
- package/test/helpers/wrong-cert.pem +0 -13
- package/test/helpers/wrong-csr.pem +0 -11
- package/test/helpers/wrong-key.pem +0 -15
- package/test/mocha.opts +0 -4
- package/test/mqtt.js +0 -230
- package/test/mqtt_store.js +0 -9
- package/test/secure_client.js +0 -188
- package/test/server.js +0 -94
- package/test/server_helpers_for_client_tests.js +0 -100
- package/test/store.js +0 -10
- package/test/typescript/broker-connect-subscribe-and-publish.js +0 -24
- package/test/typescript/broker-connect-subscribe-and-publish.js.map +0 -1
- package/test/typescript/broker-connect-subscribe-and-publish.ts +0 -25
- package/test/typescript/tsconfig.json +0 -14
- package/test/util.js +0 -13
- package/test/websocket_client.js +0 -146
package/lib/client.js
CHANGED
|
@@ -12,9 +12,10 @@ var reInterval = require('reinterval')
|
|
|
12
12
|
var validations = require('./validations')
|
|
13
13
|
var xtend = require('xtend')
|
|
14
14
|
var debug = require('debug')('mqttjs:client')
|
|
15
|
+
var nextTick = process ? process.nextTick : function (callback) { setTimeout(callback, 0) }
|
|
15
16
|
var setImmediate = global.setImmediate || function (callback) {
|
|
16
17
|
// works in node v0.8
|
|
17
|
-
|
|
18
|
+
nextTick(callback)
|
|
18
19
|
}
|
|
19
20
|
var defaultConnectOptions = {
|
|
20
21
|
keepalive: 60,
|
|
@@ -87,18 +88,18 @@ function defaultId () {
|
|
|
87
88
|
}
|
|
88
89
|
|
|
89
90
|
function sendPacket (client, packet, cb) {
|
|
90
|
-
debug('sendPacket
|
|
91
|
-
debug('sendPacket
|
|
91
|
+
debug('sendPacket :: packet: %O', packet)
|
|
92
|
+
debug('sendPacket :: emitting `packetsend`')
|
|
92
93
|
client.emit('packetsend', packet)
|
|
93
94
|
|
|
94
|
-
debug('sendPacket
|
|
95
|
+
debug('sendPacket :: writing to stream')
|
|
95
96
|
var result = mqttPacket.writeToStream(packet, client.stream, client.options)
|
|
96
|
-
debug('sendPacket
|
|
97
|
+
debug('sendPacket :: writeToStream result %s', result)
|
|
97
98
|
if (!result && cb) {
|
|
98
|
-
debug('sendPacket
|
|
99
|
+
debug('sendPacket :: handle events on `drain` once through callback.')
|
|
99
100
|
client.stream.once('drain', cb)
|
|
100
101
|
} else if (cb) {
|
|
101
|
-
debug('sendPacket
|
|
102
|
+
debug('sendPacket :: invoking cb')
|
|
102
103
|
cb()
|
|
103
104
|
}
|
|
104
105
|
}
|
|
@@ -117,7 +118,7 @@ function flush (queue) {
|
|
|
117
118
|
|
|
118
119
|
function flushVolatile (queue) {
|
|
119
120
|
if (queue) {
|
|
120
|
-
debug('flushVolatile
|
|
121
|
+
debug('flushVolatile :: deleting volatile messages from the queue and setting their callbacks as error function')
|
|
121
122
|
Object.keys(queue).forEach(function (messageId) {
|
|
122
123
|
if (queue[messageId].volatile && typeof queue[messageId].cb === 'function') {
|
|
123
124
|
queue[messageId].cb(new Error('Connection closed'))
|
|
@@ -128,7 +129,7 @@ function flushVolatile (queue) {
|
|
|
128
129
|
}
|
|
129
130
|
|
|
130
131
|
function storeAndSend (client, packet, cb, cbStorePut) {
|
|
131
|
-
debug('storeAndSend
|
|
132
|
+
debug('storeAndSend :: store packet with cmd %s to outgoingStore', packet.cmd)
|
|
132
133
|
client.outgoingStore.put(packet, function storedPacket (err) {
|
|
133
134
|
if (err) {
|
|
134
135
|
return cb && cb(err)
|
|
@@ -139,7 +140,7 @@ function storeAndSend (client, packet, cb, cbStorePut) {
|
|
|
139
140
|
}
|
|
140
141
|
|
|
141
142
|
function nop (error) {
|
|
142
|
-
debug('nop
|
|
143
|
+
debug('nop ::', error)
|
|
143
144
|
}
|
|
144
145
|
|
|
145
146
|
/**
|
|
@@ -158,7 +159,6 @@ function MqttClient (streamBuilder, options) {
|
|
|
158
159
|
}
|
|
159
160
|
|
|
160
161
|
this.options = options || {}
|
|
161
|
-
debug('MqttClient :: options: %o', options)
|
|
162
162
|
|
|
163
163
|
// Defaults
|
|
164
164
|
for (k in defaultConnectOptions) {
|
|
@@ -169,9 +169,16 @@ function MqttClient (streamBuilder, options) {
|
|
|
169
169
|
}
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
+
debug('MqttClient :: options.protocol', options.protocol)
|
|
173
|
+
debug('MqttClient :: options.protocolVersion', options.protocolVersion)
|
|
174
|
+
debug('MqttClient :: options.username', options.username)
|
|
175
|
+
debug('MqttClient :: options.keepalive', options.keepalive)
|
|
176
|
+
debug('MqttClient :: options.reconnectPeriod', options.reconnectPeriod)
|
|
177
|
+
debug('MqttClient :: options.rejectUnauthorized', options.rejectUnauthorized)
|
|
178
|
+
|
|
172
179
|
this.options.clientId = (typeof options.clientId === 'string') ? options.clientId : defaultId()
|
|
173
180
|
|
|
174
|
-
debug('MqttClient
|
|
181
|
+
debug('MqttClient :: clientId', this.options.clientId)
|
|
175
182
|
|
|
176
183
|
this.options.customHandleAcks = (options.protocolVersion === 5 && options.customHandleAcks) ? options.customHandleAcks : function () { arguments[3](0) }
|
|
177
184
|
|
|
@@ -220,12 +227,11 @@ function MqttClient (streamBuilder, options) {
|
|
|
220
227
|
|
|
221
228
|
// Send queued packets
|
|
222
229
|
this.on('connect', function () {
|
|
223
|
-
debug('MqttClient:connect')
|
|
224
230
|
var queue = this.queue
|
|
225
231
|
|
|
226
232
|
function deliver () {
|
|
227
233
|
var entry = queue.shift()
|
|
228
|
-
debug('
|
|
234
|
+
debug('deliver :: entry %o', entry)
|
|
229
235
|
var packet = null
|
|
230
236
|
|
|
231
237
|
if (!entry) {
|
|
@@ -233,7 +239,7 @@ function MqttClient (streamBuilder, options) {
|
|
|
233
239
|
}
|
|
234
240
|
|
|
235
241
|
packet = entry.packet
|
|
236
|
-
debug('
|
|
242
|
+
debug('deliver :: call _sendPacket for %o', packet)
|
|
237
243
|
that._sendPacket(
|
|
238
244
|
packet,
|
|
239
245
|
function (err) {
|
|
@@ -245,26 +251,29 @@ function MqttClient (streamBuilder, options) {
|
|
|
245
251
|
)
|
|
246
252
|
}
|
|
247
253
|
|
|
254
|
+
debug('connect :: sending queued packets')
|
|
248
255
|
deliver()
|
|
249
256
|
})
|
|
250
257
|
|
|
251
258
|
this.on('close', function () {
|
|
252
|
-
debug('
|
|
259
|
+
debug('close :: connected set to `false`')
|
|
253
260
|
this.connected = false
|
|
261
|
+
|
|
262
|
+
debug('close :: clearing connackTimer')
|
|
254
263
|
clearTimeout(this.connackTimer)
|
|
255
264
|
|
|
256
|
-
debug('
|
|
265
|
+
debug('close :: clearing ping timer')
|
|
257
266
|
if (that.pingTimer !== null) {
|
|
258
267
|
that.pingTimer.clear()
|
|
259
268
|
that.pingTimer = null
|
|
260
269
|
}
|
|
261
270
|
|
|
262
|
-
debug('
|
|
271
|
+
debug('close :: calling _setupReconnect')
|
|
263
272
|
this._setupReconnect()
|
|
264
273
|
})
|
|
265
274
|
EventEmitter.call(this)
|
|
266
275
|
|
|
267
|
-
debug('MqttClient
|
|
276
|
+
debug('MqttClient :: setting up stream')
|
|
268
277
|
this._setupStream()
|
|
269
278
|
}
|
|
270
279
|
inherits(MqttClient, EventEmitter)
|
|
@@ -282,20 +291,20 @@ MqttClient.prototype._setupStream = function () {
|
|
|
282
291
|
var completeParse = null
|
|
283
292
|
var packets = []
|
|
284
293
|
|
|
285
|
-
debug('_setupStream
|
|
294
|
+
debug('_setupStream :: calling method to clear reconnect')
|
|
286
295
|
this._clearReconnect()
|
|
287
296
|
|
|
288
|
-
debug('_setupStream
|
|
297
|
+
debug('_setupStream :: using streamBuilder provided to client to create stream')
|
|
289
298
|
this.stream = this.streamBuilder(this)
|
|
290
299
|
|
|
291
300
|
parser.on('packet', function (packet) {
|
|
292
|
-
debug('parser
|
|
301
|
+
debug('parser :: on packet push to packets array.')
|
|
293
302
|
packets.push(packet)
|
|
294
303
|
})
|
|
295
304
|
|
|
296
305
|
function nextTickWork () {
|
|
297
306
|
if (packets.length) {
|
|
298
|
-
|
|
307
|
+
nextTick(work)
|
|
299
308
|
} else {
|
|
300
309
|
var done = completeParse
|
|
301
310
|
completeParse = null
|
|
@@ -304,29 +313,30 @@ MqttClient.prototype._setupStream = function () {
|
|
|
304
313
|
}
|
|
305
314
|
|
|
306
315
|
function work () {
|
|
307
|
-
debug('
|
|
316
|
+
debug('work :: getting next packet in queue')
|
|
308
317
|
var packet = packets.shift()
|
|
309
318
|
|
|
310
319
|
if (packet) {
|
|
311
|
-
debug('
|
|
320
|
+
debug('work :: packet pulled from queue')
|
|
312
321
|
that._handlePacket(packet, nextTickWork)
|
|
313
322
|
} else {
|
|
323
|
+
debug('work :: no packets in queue')
|
|
314
324
|
var done = completeParse
|
|
315
325
|
completeParse = null
|
|
316
|
-
debug('
|
|
326
|
+
debug('work :: done flag is %s', !!(done))
|
|
317
327
|
if (done) done()
|
|
318
328
|
}
|
|
319
329
|
}
|
|
320
330
|
|
|
321
331
|
writable._write = function (buf, enc, done) {
|
|
322
332
|
completeParse = done
|
|
323
|
-
debug('
|
|
333
|
+
debug('writable stream :: parsing buffer')
|
|
324
334
|
parser.parse(buf)
|
|
325
335
|
work()
|
|
326
336
|
}
|
|
327
337
|
|
|
328
338
|
function streamErrorHandler (error) {
|
|
329
|
-
debug('
|
|
339
|
+
debug('streamErrorHandler :: error', error.message)
|
|
330
340
|
if (socketErrors.includes(error.code)) {
|
|
331
341
|
// handle error
|
|
332
342
|
debug('streamErrorHandler :: emitting error')
|
|
@@ -336,7 +346,7 @@ MqttClient.prototype._setupStream = function () {
|
|
|
336
346
|
}
|
|
337
347
|
}
|
|
338
348
|
|
|
339
|
-
debug('_setupStream
|
|
349
|
+
debug('_setupStream :: pipe stream to writable stream')
|
|
340
350
|
this.stream.pipe(writable)
|
|
341
351
|
|
|
342
352
|
// Suppress connection errors
|
|
@@ -379,13 +389,12 @@ MqttClient.prototype._setupStream = function () {
|
|
|
379
389
|
|
|
380
390
|
clearTimeout(this.connackTimer)
|
|
381
391
|
this.connackTimer = setTimeout(function () {
|
|
382
|
-
debug('connectTimeout hit
|
|
392
|
+
debug('!!connectTimeout hit!! Calling _cleanUp with force `true`')
|
|
383
393
|
that._cleanUp(true)
|
|
384
394
|
}, this.options.connectTimeout)
|
|
385
395
|
}
|
|
386
396
|
|
|
387
397
|
MqttClient.prototype._handlePacket = function (packet, done) {
|
|
388
|
-
debug('_handlePacket')
|
|
389
398
|
var options = this.options
|
|
390
399
|
|
|
391
400
|
if (options.protocolVersion === 5 && options.properties && options.properties.maximumPacketSize && options.properties.maximumPacketSize < packet.length) {
|
|
@@ -393,7 +402,7 @@ MqttClient.prototype._handlePacket = function (packet, done) {
|
|
|
393
402
|
this.end({reasonCode: 149, properties: { reasonString: 'Maximum packet size was exceeded' }})
|
|
394
403
|
return this
|
|
395
404
|
}
|
|
396
|
-
debug('_handlePacket
|
|
405
|
+
debug('_handlePacket :: emitting packetreceive')
|
|
397
406
|
this.emit('packetreceive', packet)
|
|
398
407
|
|
|
399
408
|
switch (packet.cmd) {
|
|
@@ -463,7 +472,7 @@ MqttClient.prototype._checkDisconnecting = function (callback) {
|
|
|
463
472
|
* @example client.publish('topic', 'message', console.log);
|
|
464
473
|
*/
|
|
465
474
|
MqttClient.prototype.publish = function (topic, message, opts, callback) {
|
|
466
|
-
debug('
|
|
475
|
+
debug('publish :: message `%s` to topic `%s`', message, topic)
|
|
467
476
|
var packet
|
|
468
477
|
var options = this.options
|
|
469
478
|
|
|
@@ -506,6 +515,7 @@ MqttClient.prototype.publish = function (topic, message, opts, callback) {
|
|
|
506
515
|
}
|
|
507
516
|
}
|
|
508
517
|
|
|
518
|
+
debug('publish :: qos', opts.qos)
|
|
509
519
|
switch (opts.qos) {
|
|
510
520
|
case 1:
|
|
511
521
|
case 2:
|
|
@@ -664,7 +674,7 @@ MqttClient.prototype.subscribe = function () {
|
|
|
664
674
|
|
|
665
675
|
// subscriptions to resubscribe to in case of disconnect
|
|
666
676
|
if (this.options.resubscribe) {
|
|
667
|
-
debug('subscribe
|
|
677
|
+
debug('subscribe :: resubscribe true')
|
|
668
678
|
var topics = []
|
|
669
679
|
subs.forEach(function (sub) {
|
|
670
680
|
if (that.options.reconnectPeriod > 0) {
|
|
@@ -695,7 +705,7 @@ MqttClient.prototype.subscribe = function () {
|
|
|
695
705
|
callback(err, subs)
|
|
696
706
|
}
|
|
697
707
|
}
|
|
698
|
-
debug('subscribe
|
|
708
|
+
debug('subscribe :: call _sendPacket')
|
|
699
709
|
this._sendPacket(packet)
|
|
700
710
|
|
|
701
711
|
return this
|
|
@@ -743,7 +753,7 @@ MqttClient.prototype.unsubscribe = function () {
|
|
|
743
753
|
|
|
744
754
|
if (typeof topic === 'string') {
|
|
745
755
|
packet.unsubscriptions = [topic]
|
|
746
|
-
} else if (
|
|
756
|
+
} else if (Array.isArray(topic)) {
|
|
747
757
|
packet.unsubscriptions = topic
|
|
748
758
|
}
|
|
749
759
|
|
|
@@ -762,7 +772,7 @@ MqttClient.prototype.unsubscribe = function () {
|
|
|
762
772
|
cb: callback
|
|
763
773
|
}
|
|
764
774
|
|
|
765
|
-
debug('unsubscribe:
|
|
775
|
+
debug('unsubscribe: call _sendPacket')
|
|
766
776
|
this._sendPacket(packet)
|
|
767
777
|
|
|
768
778
|
return this
|
|
@@ -780,6 +790,8 @@ MqttClient.prototype.unsubscribe = function () {
|
|
|
780
790
|
MqttClient.prototype.end = function (force, opts, cb) {
|
|
781
791
|
var that = this
|
|
782
792
|
|
|
793
|
+
debug('end :: (%s)', this.options.clientId)
|
|
794
|
+
|
|
783
795
|
if (force == null || typeof force !== 'boolean') {
|
|
784
796
|
cb = opts || nop
|
|
785
797
|
opts = force
|
|
@@ -802,15 +814,16 @@ MqttClient.prototype.end = function (force, opts, cb) {
|
|
|
802
814
|
cb = cb || nop
|
|
803
815
|
|
|
804
816
|
function closeStores () {
|
|
805
|
-
debug('end ::
|
|
817
|
+
debug('end :: closeStores: closing incoming and outgoing stores')
|
|
806
818
|
that.disconnected = true
|
|
807
|
-
that.incomingStore.close(function () {
|
|
808
|
-
that.outgoingStore.close(function () {
|
|
809
|
-
debug('end ::
|
|
819
|
+
that.incomingStore.close(function (e1) {
|
|
820
|
+
that.outgoingStore.close(function (e2) {
|
|
821
|
+
debug('end :: closeStores: emitting end')
|
|
810
822
|
that.emit('end')
|
|
811
823
|
if (cb) {
|
|
812
|
-
|
|
813
|
-
|
|
824
|
+
let err = e1 || e2
|
|
825
|
+
debug('end :: closeStores: invoking callback with args')
|
|
826
|
+
cb(err)
|
|
814
827
|
}
|
|
815
828
|
})
|
|
816
829
|
})
|
|
@@ -826,8 +839,8 @@ MqttClient.prototype.end = function (force, opts, cb) {
|
|
|
826
839
|
debug('end :: (%s) :: finish :: calling _cleanUp with force %s', that.options.clientId, force)
|
|
827
840
|
that._cleanUp(force, () => {
|
|
828
841
|
debug('end :: finish :: calling process.nextTick on closeStores')
|
|
829
|
-
// var boundProcess =
|
|
830
|
-
|
|
842
|
+
// var boundProcess = nextTick.bind(null, closeStores)
|
|
843
|
+
nextTick(closeStores.bind(that))
|
|
831
844
|
}, opts)
|
|
832
845
|
}
|
|
833
846
|
|
|
@@ -924,18 +937,18 @@ MqttClient.prototype._reconnect = function () {
|
|
|
924
937
|
* _setupReconnect - setup reconnect timer
|
|
925
938
|
*/
|
|
926
939
|
MqttClient.prototype._setupReconnect = function () {
|
|
927
|
-
debug('_setupReconnect')
|
|
928
940
|
var that = this
|
|
929
941
|
|
|
930
942
|
if (!that.disconnecting && !that.reconnectTimer && (that.options.reconnectPeriod > 0)) {
|
|
931
943
|
if (!this.reconnecting) {
|
|
932
|
-
debug('_setupReconnect ::
|
|
944
|
+
debug('_setupReconnect :: emit `offline` state')
|
|
933
945
|
this.emit('offline')
|
|
946
|
+
debug('_setupReconnect :: set `reconnecting` to `true`')
|
|
934
947
|
this.reconnecting = true
|
|
935
948
|
}
|
|
936
949
|
debug('_setupReconnect :: setting reconnectTimer for %d ms', that.options.reconnectPeriod)
|
|
937
950
|
that.reconnectTimer = setInterval(function () {
|
|
938
|
-
debug('reconnectTimer
|
|
951
|
+
debug('reconnectTimer :: reconnect triggered!')
|
|
939
952
|
that._reconnect()
|
|
940
953
|
}, that.options.reconnectPeriod)
|
|
941
954
|
} else {
|
|
@@ -947,7 +960,7 @@ MqttClient.prototype._setupReconnect = function () {
|
|
|
947
960
|
* _clearReconnect - clear the reconnect timer
|
|
948
961
|
*/
|
|
949
962
|
MqttClient.prototype._clearReconnect = function () {
|
|
950
|
-
debug('_clearReconnect
|
|
963
|
+
debug('_clearReconnect : clearing reconnect timer')
|
|
951
964
|
if (this.reconnectTimer) {
|
|
952
965
|
clearInterval(this.reconnectTimer)
|
|
953
966
|
this.reconnectTimer = null
|
|
@@ -961,20 +974,20 @@ MqttClient.prototype._clearReconnect = function () {
|
|
|
961
974
|
MqttClient.prototype._cleanUp = function (forced, done) {
|
|
962
975
|
var opts = arguments[2]
|
|
963
976
|
if (done) {
|
|
964
|
-
debug('_cleanUp
|
|
977
|
+
debug('_cleanUp :: done callback provided for on stream close')
|
|
965
978
|
this.stream.on('close', done)
|
|
966
979
|
}
|
|
967
980
|
|
|
968
|
-
debug('_cleanUp
|
|
981
|
+
debug('_cleanUp :: forced? %s', forced)
|
|
969
982
|
if (forced) {
|
|
970
983
|
if ((this.options.reconnectPeriod === 0) && this.options.clean) {
|
|
971
984
|
flush(this.outgoing)
|
|
972
985
|
}
|
|
973
|
-
debug('(%s)
|
|
986
|
+
debug('_cleanUp :: (%s) :: destroying stream', this.options.clientId)
|
|
974
987
|
this.stream.destroy()
|
|
975
988
|
} else {
|
|
976
989
|
var packet = xtend({ cmd: 'disconnect' }, opts)
|
|
977
|
-
debug('(%s)
|
|
990
|
+
debug('_cleanUp :: (%s) :: call _sendPacket with disconnect packet', this.options.clientId)
|
|
978
991
|
this._sendPacket(
|
|
979
992
|
packet,
|
|
980
993
|
setImmediate.bind(
|
|
@@ -985,19 +998,19 @@ MqttClient.prototype._cleanUp = function (forced, done) {
|
|
|
985
998
|
}
|
|
986
999
|
|
|
987
1000
|
if (!this.disconnecting) {
|
|
988
|
-
debug('_cleanUp
|
|
1001
|
+
debug('_cleanUp :: client not disconnecting. Clearing and resetting reconnect.')
|
|
989
1002
|
this._clearReconnect()
|
|
990
1003
|
this._setupReconnect()
|
|
991
1004
|
}
|
|
992
1005
|
|
|
993
1006
|
if (this.pingTimer !== null) {
|
|
994
|
-
debug('_cleanUp
|
|
1007
|
+
debug('_cleanUp :: clearing pingTimer')
|
|
995
1008
|
this.pingTimer.clear()
|
|
996
1009
|
this.pingTimer = null
|
|
997
1010
|
}
|
|
998
1011
|
|
|
999
1012
|
if (done && !this.connected) {
|
|
1000
|
-
debug('(%s)
|
|
1013
|
+
debug('_cleanUp :: (%s) :: removing stream `done` callback `close` listener', this.options.clientId)
|
|
1001
1014
|
this.stream.removeListener('close', done)
|
|
1002
1015
|
done()
|
|
1003
1016
|
}
|
|
@@ -1087,6 +1100,7 @@ MqttClient.prototype._storePacket = function (packet, cb, cbStorePut) {
|
|
|
1087
1100
|
* @api private
|
|
1088
1101
|
*/
|
|
1089
1102
|
MqttClient.prototype._setupPingTimer = function () {
|
|
1103
|
+
debug('_setupPingTimer :: keepalive %d (seconds)', this.options.keepalive)
|
|
1090
1104
|
var that = this
|
|
1091
1105
|
|
|
1092
1106
|
if (!this.pingTimer && this.options.keepalive) {
|
|
@@ -1285,7 +1299,6 @@ MqttClient.prototype.handleMessage = function (packet, callback) {
|
|
|
1285
1299
|
*/
|
|
1286
1300
|
|
|
1287
1301
|
MqttClient.prototype._handleAck = function (packet) {
|
|
1288
|
-
debug('handling ack packet')
|
|
1289
1302
|
/* eslint no-fallthrough: "off" */
|
|
1290
1303
|
var messageId = packet.messageId
|
|
1291
1304
|
var type = packet.cmd
|
|
@@ -1295,13 +1308,13 @@ MqttClient.prototype._handleAck = function (packet) {
|
|
|
1295
1308
|
var err
|
|
1296
1309
|
|
|
1297
1310
|
if (!cb) {
|
|
1298
|
-
debug('Server sent an ack in error. Ignoring.')
|
|
1311
|
+
debug('_handleAck :: Server sent an ack in error. Ignoring.')
|
|
1299
1312
|
// Server sent an ack in error, ignore it.
|
|
1300
1313
|
return
|
|
1301
1314
|
}
|
|
1302
1315
|
|
|
1303
1316
|
// Process
|
|
1304
|
-
debug('
|
|
1317
|
+
debug('_handleAck :: packet type', type)
|
|
1305
1318
|
switch (type) {
|
|
1306
1319
|
case 'pubcomp':
|
|
1307
1320
|
// same thing as puback for QoS 2
|
package/lib/connect/index.js
CHANGED
|
@@ -4,9 +4,12 @@ var MqttClient = require('../client')
|
|
|
4
4
|
var Store = require('../store')
|
|
5
5
|
var url = require('url')
|
|
6
6
|
var xtend = require('xtend')
|
|
7
|
+
var debug = require('debug')('mqttjs')
|
|
8
|
+
|
|
7
9
|
var protocols = {}
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
// eslint-disable-next-line camelcase
|
|
12
|
+
if ((typeof process !== 'undefined' && process.title !== 'browser') || typeof __webpack_require__ === 'function') {
|
|
10
13
|
protocols.mqtt = require('./tcp')
|
|
11
14
|
protocols.tcp = require('./tcp')
|
|
12
15
|
protocols.ssl = require('./tls')
|
|
@@ -48,6 +51,7 @@ function parseAuthOptions (opts) {
|
|
|
48
51
|
* @param {Object} opts - see MqttClient#constructor
|
|
49
52
|
*/
|
|
50
53
|
function connect (brokerUrl, opts) {
|
|
54
|
+
debug('connecting to an MQTT broker...')
|
|
51
55
|
if ((typeof brokerUrl === 'object') && !opts) {
|
|
52
56
|
opts = brokerUrl
|
|
53
57
|
brokerUrl = null
|
|
@@ -98,7 +102,7 @@ function connect (brokerUrl, opts) {
|
|
|
98
102
|
}
|
|
99
103
|
}
|
|
100
104
|
} else {
|
|
101
|
-
//
|
|
105
|
+
// A cert and key was provided, however no protocol was specified, so we will throw an error.
|
|
102
106
|
throw new Error('Missing secure protocol key')
|
|
103
107
|
}
|
|
104
108
|
}
|
|
@@ -145,6 +149,7 @@ function connect (brokerUrl, opts) {
|
|
|
145
149
|
client._reconnectCount++
|
|
146
150
|
}
|
|
147
151
|
|
|
152
|
+
debug('calling streambuilder for', opts.protocol)
|
|
148
153
|
return protocols[opts.protocol](client, opts)
|
|
149
154
|
}
|
|
150
155
|
var client = new MqttClient(wrapper, opts)
|
package/lib/connect/tcp.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
var net = require('net')
|
|
3
|
+
var debug = require('debug')('mqttjs:tcp')
|
|
3
4
|
|
|
4
5
|
/*
|
|
5
6
|
variables port and host can be removed since
|
|
@@ -13,6 +14,7 @@ function streamBuilder (client, opts) {
|
|
|
13
14
|
port = opts.port
|
|
14
15
|
host = opts.hostname
|
|
15
16
|
|
|
17
|
+
debug('port %d and host %s', port, host)
|
|
16
18
|
return net.createConnection(port, host)
|
|
17
19
|
}
|
|
18
20
|
|
package/lib/connect/tls.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
var tls = require('tls')
|
|
3
|
+
var debug = require('debug')('mqttjs:tls')
|
|
3
4
|
|
|
4
5
|
function buildBuilder (mqttClient, opts) {
|
|
5
6
|
var connection
|
|
@@ -11,6 +12,8 @@ function buildBuilder (mqttClient, opts) {
|
|
|
11
12
|
|
|
12
13
|
delete opts.path
|
|
13
14
|
|
|
15
|
+
debug('port %d host %s rejectUnauthorized %b', opts.port, opts.host, opts.rejectUnauthorized)
|
|
16
|
+
|
|
14
17
|
connection = tls.connect(opts)
|
|
15
18
|
/* eslint no-use-before-define: [2, "nofunc"] */
|
|
16
19
|
connection.on('secureConnect', function () {
|