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/test/client.js
DELETED
|
@@ -1,540 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var mqtt = require('..')
|
|
4
|
-
var assert = require('chai').assert
|
|
5
|
-
const { fork } = require('child_process')
|
|
6
|
-
var path = require('path')
|
|
7
|
-
var abstractClientTests = require('./abstract_client')
|
|
8
|
-
var net = require('net')
|
|
9
|
-
var eos = require('end-of-stream')
|
|
10
|
-
var mqttPacket = require('mqtt-packet')
|
|
11
|
-
var Buffer = require('safe-buffer').Buffer
|
|
12
|
-
var Duplex = require('readable-stream').Duplex
|
|
13
|
-
var Connection = require('mqtt-connection')
|
|
14
|
-
var MqttServer = require('./server').MqttServer
|
|
15
|
-
var util = require('util')
|
|
16
|
-
var ports = require('./helpers/port_list')
|
|
17
|
-
var serverBuilder = require('./server_helpers_for_client_tests').serverBuilder
|
|
18
|
-
var debug = require('debug')('TEST:client')
|
|
19
|
-
|
|
20
|
-
describe('MqttClient', function () {
|
|
21
|
-
var client
|
|
22
|
-
var server = serverBuilder()
|
|
23
|
-
var config = {protocol: 'mqtt', port: ports.PORT}
|
|
24
|
-
server.listen(ports.PORT)
|
|
25
|
-
|
|
26
|
-
after(function () {
|
|
27
|
-
// clean up and make sure the server is no longer listening...
|
|
28
|
-
if (server.listening) {
|
|
29
|
-
server.close()
|
|
30
|
-
}
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
abstractClientTests(server, config)
|
|
34
|
-
|
|
35
|
-
describe('creating', function () {
|
|
36
|
-
it('should allow instantiation of MqttClient without the \'new\' operator', function (done) {
|
|
37
|
-
try {
|
|
38
|
-
client = mqtt.MqttClient(function () {
|
|
39
|
-
throw Error('break')
|
|
40
|
-
}, {})
|
|
41
|
-
client.end()
|
|
42
|
-
} catch (err) {
|
|
43
|
-
assert.strictEqual(err.message, 'break')
|
|
44
|
-
done()
|
|
45
|
-
}
|
|
46
|
-
})
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
describe('message ids', function () {
|
|
50
|
-
it('should increment the message id', function () {
|
|
51
|
-
client = mqtt.connect(config)
|
|
52
|
-
var currentId = client._nextId()
|
|
53
|
-
|
|
54
|
-
assert.equal(client._nextId(), currentId + 1)
|
|
55
|
-
client.end()
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
it('should return 1 once the internal counter reached limit', function () {
|
|
59
|
-
client = mqtt.connect(config)
|
|
60
|
-
client.nextId = 65535
|
|
61
|
-
|
|
62
|
-
assert.equal(client._nextId(), 65535)
|
|
63
|
-
assert.equal(client._nextId(), 1)
|
|
64
|
-
client.end()
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
it('should return 65535 for last message id once the internal counter reached limit', function () {
|
|
68
|
-
client = mqtt.connect(config)
|
|
69
|
-
client.nextId = 65535
|
|
70
|
-
|
|
71
|
-
assert.equal(client._nextId(), 65535)
|
|
72
|
-
assert.equal(client.getLastMessageId(), 65535)
|
|
73
|
-
assert.equal(client._nextId(), 1)
|
|
74
|
-
assert.equal(client.getLastMessageId(), 1)
|
|
75
|
-
client.end()
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
it('should not throw an error if packet\'s messageId is not found when receiving a pubrel packet', function (done) {
|
|
79
|
-
var server2 = new MqttServer(function (serverClient) {
|
|
80
|
-
serverClient.on('connect', function (packet) {
|
|
81
|
-
serverClient.connack({returnCode: 0})
|
|
82
|
-
serverClient.pubrel({ messageId: Math.floor(Math.random() * 9000) + 1000 })
|
|
83
|
-
})
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
server2.listen(ports.PORTAND49, function () {
|
|
87
|
-
client = mqtt.connect({
|
|
88
|
-
port: ports.PORTAND49,
|
|
89
|
-
host: 'localhost'
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
client.on('packetsend', function (packet) {
|
|
93
|
-
if (packet.cmd === 'pubcomp') {
|
|
94
|
-
client.end()
|
|
95
|
-
server2.close()
|
|
96
|
-
done()
|
|
97
|
-
}
|
|
98
|
-
})
|
|
99
|
-
})
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
it('should not go overflow if the TCP frame contains a lot of PUBLISH packets', function (done) {
|
|
103
|
-
var parser = mqttPacket.parser()
|
|
104
|
-
var count = 0
|
|
105
|
-
var max = 1000
|
|
106
|
-
var duplex = new Duplex({
|
|
107
|
-
read: function (n) {},
|
|
108
|
-
write: function (chunk, enc, cb) {
|
|
109
|
-
parser.parse(chunk)
|
|
110
|
-
cb() // nothing to do
|
|
111
|
-
}
|
|
112
|
-
})
|
|
113
|
-
client = new mqtt.MqttClient(function () {
|
|
114
|
-
return duplex
|
|
115
|
-
}, {})
|
|
116
|
-
|
|
117
|
-
client.on('message', function (t, p, packet) {
|
|
118
|
-
if (++count === max) {
|
|
119
|
-
done()
|
|
120
|
-
}
|
|
121
|
-
})
|
|
122
|
-
|
|
123
|
-
parser.on('packet', function (packet) {
|
|
124
|
-
var packets = []
|
|
125
|
-
|
|
126
|
-
if (packet.cmd === 'connect') {
|
|
127
|
-
duplex.push(mqttPacket.generate({
|
|
128
|
-
cmd: 'connack',
|
|
129
|
-
sessionPresent: false,
|
|
130
|
-
returnCode: 0
|
|
131
|
-
}))
|
|
132
|
-
|
|
133
|
-
for (var i = 0; i < max; i++) {
|
|
134
|
-
packets.push(mqttPacket.generate({
|
|
135
|
-
cmd: 'publish',
|
|
136
|
-
topic: Buffer.from('hello'),
|
|
137
|
-
payload: Buffer.from('world'),
|
|
138
|
-
retain: false,
|
|
139
|
-
dup: false,
|
|
140
|
-
messageId: i + 1,
|
|
141
|
-
qos: 1
|
|
142
|
-
}))
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
duplex.push(Buffer.concat(packets))
|
|
146
|
-
}
|
|
147
|
-
})
|
|
148
|
-
})
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
describe('flushing', function () {
|
|
152
|
-
it('should attempt to complete pending unsub and send on ping timeout', function (done) {
|
|
153
|
-
this.timeout(10000)
|
|
154
|
-
var server3 = new MqttServer(function (client) {
|
|
155
|
-
client.on('connect', function (packet) {
|
|
156
|
-
client.connack({returnCode: 0})
|
|
157
|
-
})
|
|
158
|
-
}).listen(ports.PORTAND72)
|
|
159
|
-
|
|
160
|
-
var pubCallbackCalled = false
|
|
161
|
-
var unsubscribeCallbackCalled = false
|
|
162
|
-
client = mqtt.connect({
|
|
163
|
-
port: ports.PORTAND72,
|
|
164
|
-
host: 'localhost',
|
|
165
|
-
keepalive: 1,
|
|
166
|
-
connectTimeout: 350,
|
|
167
|
-
reconnectPeriod: 0
|
|
168
|
-
})
|
|
169
|
-
client.once('connect', () => {
|
|
170
|
-
client.publish('fakeTopic', 'fakeMessage', {qos: 1}, (err, result) => {
|
|
171
|
-
assert.exists(err)
|
|
172
|
-
pubCallbackCalled = true
|
|
173
|
-
})
|
|
174
|
-
client.unsubscribe('fakeTopic', (err, result) => {
|
|
175
|
-
assert.exists(err)
|
|
176
|
-
unsubscribeCallbackCalled = true
|
|
177
|
-
})
|
|
178
|
-
setTimeout(() => {
|
|
179
|
-
client.end(() => {
|
|
180
|
-
assert.strictEqual(pubCallbackCalled && unsubscribeCallbackCalled, true, 'callbacks not invoked')
|
|
181
|
-
server3.close()
|
|
182
|
-
done()
|
|
183
|
-
})
|
|
184
|
-
}, 5000)
|
|
185
|
-
})
|
|
186
|
-
})
|
|
187
|
-
})
|
|
188
|
-
|
|
189
|
-
describe('reconnecting', function () {
|
|
190
|
-
it('should attempt to reconnect once server is down', function (done) {
|
|
191
|
-
this.timeout(30000)
|
|
192
|
-
|
|
193
|
-
var innerServer = fork(path.join(__dirname, 'helpers', 'server_process.js'), { execArgv: ['--inspect'] })
|
|
194
|
-
innerServer.on('close', (code) => {
|
|
195
|
-
if (code) {
|
|
196
|
-
done(util.format('child process closed with code %d', code))
|
|
197
|
-
}
|
|
198
|
-
})
|
|
199
|
-
|
|
200
|
-
innerServer.on('exit', (code) => {
|
|
201
|
-
if (code) {
|
|
202
|
-
done(util.format('child process exited with code %d', code))
|
|
203
|
-
}
|
|
204
|
-
})
|
|
205
|
-
|
|
206
|
-
client = mqtt.connect({ port: 3000, host: 'localhost', keepalive: 1 })
|
|
207
|
-
client.once('connect', function () {
|
|
208
|
-
innerServer.kill('SIGINT') // mocks server shutdown
|
|
209
|
-
client.once('close', function () {
|
|
210
|
-
assert.exists(client.reconnectTimer)
|
|
211
|
-
client.end(true, done)
|
|
212
|
-
})
|
|
213
|
-
})
|
|
214
|
-
})
|
|
215
|
-
|
|
216
|
-
it('should reconnect to multiple host-ports-protocol combinations if servers is passed', function (done) {
|
|
217
|
-
this.timeout(15000)
|
|
218
|
-
var actualURL41 = 'wss://localhost:9917/'
|
|
219
|
-
var actualURL42 = 'ws://localhost:9918/'
|
|
220
|
-
var serverPort41 = serverBuilder(true).listen(ports.PORTAND41)
|
|
221
|
-
var serverPort42 = serverBuilder(true).listen(ports.PORTAND42)
|
|
222
|
-
|
|
223
|
-
serverPort42.on('listening', function () {
|
|
224
|
-
client = mqtt.connect({
|
|
225
|
-
protocol: 'wss',
|
|
226
|
-
servers: [
|
|
227
|
-
{ port: ports.PORTAND41, host: 'localhost' },
|
|
228
|
-
{ port: ports.PORTAND42, host: 'localhost', protocol: 'ws' }
|
|
229
|
-
],
|
|
230
|
-
keepalive: 50
|
|
231
|
-
})
|
|
232
|
-
serverPort41.once('client', function () {
|
|
233
|
-
assert.equal(client.stream.socket.url, actualURL41, 'Protocol for second client should use the default protocol: wss, on port: port + 41.')
|
|
234
|
-
client.end(true, done)
|
|
235
|
-
serverPort41.close()
|
|
236
|
-
})
|
|
237
|
-
serverPort42.on('client', function (c) {
|
|
238
|
-
assert.equal(client.stream.socket.url, actualURL42, 'Protocol for connection should use ws, on port: port + 42.')
|
|
239
|
-
c.stream.destroy()
|
|
240
|
-
serverPort42.close()
|
|
241
|
-
})
|
|
242
|
-
|
|
243
|
-
client.once('connect', function () {
|
|
244
|
-
client.stream.destroy()
|
|
245
|
-
})
|
|
246
|
-
})
|
|
247
|
-
})
|
|
248
|
-
|
|
249
|
-
it('should reconnect if a connack is not received in an interval', function (done) {
|
|
250
|
-
this.timeout(2000)
|
|
251
|
-
|
|
252
|
-
var server2 = net.createServer().listen(ports.PORTAND43)
|
|
253
|
-
|
|
254
|
-
server2.on('connection', function (c) {
|
|
255
|
-
eos(c, function () {
|
|
256
|
-
server2.close()
|
|
257
|
-
})
|
|
258
|
-
})
|
|
259
|
-
|
|
260
|
-
server2.on('listening', function () {
|
|
261
|
-
client = mqtt.connect({
|
|
262
|
-
servers: [
|
|
263
|
-
{ port: ports.PORTAND43, host: 'localhost_fake' },
|
|
264
|
-
{ port: ports.PORT, host: 'localhost' }
|
|
265
|
-
],
|
|
266
|
-
connectTimeout: 500
|
|
267
|
-
})
|
|
268
|
-
|
|
269
|
-
server.once('client', function () {
|
|
270
|
-
client.end(true, (err) => {
|
|
271
|
-
done(err)
|
|
272
|
-
})
|
|
273
|
-
})
|
|
274
|
-
|
|
275
|
-
client.once('connect', function () {
|
|
276
|
-
client.stream.destroy()
|
|
277
|
-
})
|
|
278
|
-
})
|
|
279
|
-
})
|
|
280
|
-
|
|
281
|
-
it('should not be cleared by the connack timer', function (done) {
|
|
282
|
-
this.timeout(4000)
|
|
283
|
-
|
|
284
|
-
var server2 = net.createServer().listen(ports.PORTAND44)
|
|
285
|
-
|
|
286
|
-
server2.on('connection', function (c) {
|
|
287
|
-
c.destroy()
|
|
288
|
-
})
|
|
289
|
-
|
|
290
|
-
server2.once('listening', function () {
|
|
291
|
-
var reconnects = 0
|
|
292
|
-
var connectTimeout = 1000
|
|
293
|
-
var reconnectPeriod = 100
|
|
294
|
-
var expectedReconnects = Math.floor(connectTimeout / reconnectPeriod)
|
|
295
|
-
client = mqtt.connect({
|
|
296
|
-
port: ports.PORTAND44,
|
|
297
|
-
host: 'localhost',
|
|
298
|
-
connectTimeout: connectTimeout,
|
|
299
|
-
reconnectPeriod: reconnectPeriod
|
|
300
|
-
})
|
|
301
|
-
|
|
302
|
-
client.on('reconnect', function () {
|
|
303
|
-
reconnects++
|
|
304
|
-
if (reconnects >= expectedReconnects) {
|
|
305
|
-
client.end(true, done)
|
|
306
|
-
}
|
|
307
|
-
})
|
|
308
|
-
})
|
|
309
|
-
})
|
|
310
|
-
|
|
311
|
-
it('should not keep requeueing the first message when offline', function (done) {
|
|
312
|
-
this.timeout(2500)
|
|
313
|
-
|
|
314
|
-
var server2 = serverBuilder().listen(ports.PORTAND45)
|
|
315
|
-
client = mqtt.connect({
|
|
316
|
-
port: ports.PORTAND45,
|
|
317
|
-
host: 'localhost',
|
|
318
|
-
connectTimeout: 350,
|
|
319
|
-
reconnectPeriod: 300
|
|
320
|
-
})
|
|
321
|
-
|
|
322
|
-
server2.on('client', function (serverClient) {
|
|
323
|
-
client.publish('hello', 'world', { qos: 1 }, function () {
|
|
324
|
-
serverClient.destroy()
|
|
325
|
-
server2.close(() => {
|
|
326
|
-
debug('now publishing message in an offline state')
|
|
327
|
-
client.publish('hello', 'world', { qos: 1 })
|
|
328
|
-
})
|
|
329
|
-
})
|
|
330
|
-
})
|
|
331
|
-
|
|
332
|
-
setTimeout(function () {
|
|
333
|
-
if (client.queue.length === 0) {
|
|
334
|
-
debug('calling final client.end()')
|
|
335
|
-
client.end(true, (err) => done(err))
|
|
336
|
-
} else {
|
|
337
|
-
debug('calling client.end()')
|
|
338
|
-
client.end(true)
|
|
339
|
-
}
|
|
340
|
-
}, 2000)
|
|
341
|
-
})
|
|
342
|
-
|
|
343
|
-
it('should not send the same subscribe multiple times on a flaky connection', function (done) {
|
|
344
|
-
this.timeout(3500)
|
|
345
|
-
|
|
346
|
-
var KILL_COUNT = 4
|
|
347
|
-
var killedConnections = 0
|
|
348
|
-
var subIds = {}
|
|
349
|
-
client = mqtt.connect({
|
|
350
|
-
port: ports.PORTAND46,
|
|
351
|
-
host: 'localhost',
|
|
352
|
-
connectTimeout: 350,
|
|
353
|
-
reconnectPeriod: 300
|
|
354
|
-
})
|
|
355
|
-
|
|
356
|
-
var server2 = new MqttServer(function (serverClient) {
|
|
357
|
-
serverClient.on('error', function () {})
|
|
358
|
-
debug('setting serverClient connect callback')
|
|
359
|
-
serverClient.on('connect', function (packet) {
|
|
360
|
-
if (packet.clientId === 'invalid') {
|
|
361
|
-
debug('connack with returnCode 2')
|
|
362
|
-
serverClient.connack({returnCode: 2})
|
|
363
|
-
} else {
|
|
364
|
-
debug('connack with returnCode 0')
|
|
365
|
-
serverClient.connack({returnCode: 0})
|
|
366
|
-
}
|
|
367
|
-
})
|
|
368
|
-
}).listen(ports.PORTAND46)
|
|
369
|
-
|
|
370
|
-
server2.on('client', function (serverClient) {
|
|
371
|
-
debug('client received on server2.')
|
|
372
|
-
debug('subscribing to topic `topic`')
|
|
373
|
-
client.subscribe('topic', function () {
|
|
374
|
-
debug('once subscribed to topic, end client, destroy serverClient, and close server.')
|
|
375
|
-
serverClient.destroy()
|
|
376
|
-
server2.close(() => { client.end(true, done) })
|
|
377
|
-
})
|
|
378
|
-
|
|
379
|
-
serverClient.on('subscribe', function (packet) {
|
|
380
|
-
if (killedConnections < KILL_COUNT) {
|
|
381
|
-
// Kill the first few sub attempts to simulate a flaky connection
|
|
382
|
-
killedConnections++
|
|
383
|
-
serverClient.destroy()
|
|
384
|
-
} else {
|
|
385
|
-
// Keep track of acks
|
|
386
|
-
if (!subIds[packet.messageId]) {
|
|
387
|
-
subIds[packet.messageId] = 0
|
|
388
|
-
}
|
|
389
|
-
subIds[packet.messageId]++
|
|
390
|
-
if (subIds[packet.messageId] > 1) {
|
|
391
|
-
done(new Error('Multiple duplicate acked subscriptions received for messageId ' + packet.messageId))
|
|
392
|
-
client.end(true)
|
|
393
|
-
serverClient.end()
|
|
394
|
-
server2.destroy()
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
serverClient.suback({
|
|
398
|
-
messageId: packet.messageId,
|
|
399
|
-
granted: packet.subscriptions.map(function (e) {
|
|
400
|
-
return e.qos
|
|
401
|
-
})
|
|
402
|
-
})
|
|
403
|
-
}
|
|
404
|
-
})
|
|
405
|
-
})
|
|
406
|
-
})
|
|
407
|
-
|
|
408
|
-
it('should not fill the queue of subscribes if it cannot connect', function (done) {
|
|
409
|
-
this.timeout(2500)
|
|
410
|
-
var server2 = net.createServer(function (stream) {
|
|
411
|
-
var serverClient = new Connection(stream)
|
|
412
|
-
|
|
413
|
-
serverClient.on('error', function (e) { /* do nothing */ })
|
|
414
|
-
serverClient.on('connect', function (packet) {
|
|
415
|
-
serverClient.connack({returnCode: 0})
|
|
416
|
-
serverClient.destroy()
|
|
417
|
-
})
|
|
418
|
-
})
|
|
419
|
-
|
|
420
|
-
server2.listen(ports.PORTAND48, function () {
|
|
421
|
-
client = mqtt.connect({
|
|
422
|
-
port: ports.PORTAND48,
|
|
423
|
-
host: 'localhost',
|
|
424
|
-
connectTimeout: 350,
|
|
425
|
-
reconnectPeriod: 300
|
|
426
|
-
})
|
|
427
|
-
|
|
428
|
-
client.subscribe('hello')
|
|
429
|
-
|
|
430
|
-
setTimeout(function () {
|
|
431
|
-
assert.equal(client.queue.length, 1)
|
|
432
|
-
client.end(true, () => {
|
|
433
|
-
done()
|
|
434
|
-
})
|
|
435
|
-
}, 1000)
|
|
436
|
-
})
|
|
437
|
-
})
|
|
438
|
-
|
|
439
|
-
it('should not send the same publish multiple times on a flaky connection', function (done) {
|
|
440
|
-
this.timeout(3500)
|
|
441
|
-
|
|
442
|
-
var KILL_COUNT = 4
|
|
443
|
-
var killedConnections = 0
|
|
444
|
-
var pubIds = {}
|
|
445
|
-
client = mqtt.connect({
|
|
446
|
-
port: ports.PORTAND47,
|
|
447
|
-
host: 'localhost',
|
|
448
|
-
connectTimeout: 350,
|
|
449
|
-
reconnectPeriod: 300
|
|
450
|
-
})
|
|
451
|
-
|
|
452
|
-
var server2 = net.createServer(function (stream) {
|
|
453
|
-
var serverClient = new Connection(stream)
|
|
454
|
-
serverClient.on('error', function () {})
|
|
455
|
-
serverClient.on('connect', function (packet) {
|
|
456
|
-
if (packet.clientId === 'invalid') {
|
|
457
|
-
serverClient.connack({returnCode: 2})
|
|
458
|
-
} else {
|
|
459
|
-
serverClient.connack({returnCode: 0})
|
|
460
|
-
}
|
|
461
|
-
})
|
|
462
|
-
|
|
463
|
-
this.emit('client', serverClient)
|
|
464
|
-
}).listen(ports.PORTAND47)
|
|
465
|
-
|
|
466
|
-
server2.on('client', function (serverClient) {
|
|
467
|
-
client.publish('topic', 'data', { qos: 1 }, function () {
|
|
468
|
-
serverClient.destroy()
|
|
469
|
-
server2.close()
|
|
470
|
-
client.end(true, done)
|
|
471
|
-
})
|
|
472
|
-
|
|
473
|
-
serverClient.on('publish', function onPublish (packet) {
|
|
474
|
-
if (killedConnections < KILL_COUNT) {
|
|
475
|
-
// Kill the first few pub attempts to simulate a flaky connection
|
|
476
|
-
killedConnections++
|
|
477
|
-
serverClient.destroy()
|
|
478
|
-
|
|
479
|
-
// to avoid receiving inflight messages
|
|
480
|
-
serverClient.removeListener('publish', onPublish)
|
|
481
|
-
} else {
|
|
482
|
-
// Keep track of acks
|
|
483
|
-
if (!pubIds[packet.messageId]) {
|
|
484
|
-
pubIds[packet.messageId] = 0
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
pubIds[packet.messageId]++
|
|
488
|
-
|
|
489
|
-
if (pubIds[packet.messageId] > 1) {
|
|
490
|
-
done(new Error('Multiple duplicate acked publishes received for messageId ' + packet.messageId))
|
|
491
|
-
client.end(true)
|
|
492
|
-
serverClient.destroy()
|
|
493
|
-
server2.destroy()
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
serverClient.puback(packet)
|
|
497
|
-
}
|
|
498
|
-
})
|
|
499
|
-
})
|
|
500
|
-
})
|
|
501
|
-
})
|
|
502
|
-
|
|
503
|
-
it('check emit error on checkDisconnection w/o callback', function (done) {
|
|
504
|
-
this.timeout(15000)
|
|
505
|
-
|
|
506
|
-
var server118 = new MqttServer(function (client) {
|
|
507
|
-
client.on('connect', function (packet) {
|
|
508
|
-
client.connack({
|
|
509
|
-
reasonCode: 0
|
|
510
|
-
})
|
|
511
|
-
})
|
|
512
|
-
client.on('publish', function (packet) {
|
|
513
|
-
setImmediate(function () {
|
|
514
|
-
packet.reasonCode = 0
|
|
515
|
-
client.puback(packet)
|
|
516
|
-
})
|
|
517
|
-
})
|
|
518
|
-
}).listen(ports.PORTAND118)
|
|
519
|
-
|
|
520
|
-
var opts = {
|
|
521
|
-
host: 'localhost',
|
|
522
|
-
port: ports.PORTAND118,
|
|
523
|
-
protocolVersion: 5
|
|
524
|
-
}
|
|
525
|
-
client = mqtt.connect(opts)
|
|
526
|
-
|
|
527
|
-
// wait for the client to receive an error...
|
|
528
|
-
client.on('error', function (error) {
|
|
529
|
-
assert.equal(error.message, 'client disconnecting')
|
|
530
|
-
server118.close()
|
|
531
|
-
done()
|
|
532
|
-
})
|
|
533
|
-
client.on('connect', function () {
|
|
534
|
-
client.end(function () {
|
|
535
|
-
client._checkDisconnecting()
|
|
536
|
-
})
|
|
537
|
-
server118.close()
|
|
538
|
-
})
|
|
539
|
-
})
|
|
540
|
-
})
|