mqtt 4.2.8 → 4.3.0
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/CHANGELOG.md +62 -22
- package/CONTRIBUTING.md +27 -27
- package/LICENSE.md +15 -15
- package/README.md +843 -792
- package/bin/mqtt.js +27 -27
- package/bin/pub.js +146 -146
- package/bin/sub.js +123 -123
- package/dist/mqtt.js +12624 -4503
- package/dist/mqtt.min.js +1 -1
- package/doc/help.txt +8 -8
- package/doc/publish.txt +26 -26
- package/doc/subscribe.txt +26 -26
- package/lib/client.js +1890 -1581
- package/lib/connect/ali.js +128 -128
- package/lib/connect/index.js +165 -164
- package/lib/connect/tcp.js +20 -21
- package/lib/connect/tls.js +48 -45
- package/lib/connect/ws.js +256 -256
- package/lib/connect/wx.js +132 -134
- package/lib/default-message-id-provider.js +69 -0
- package/lib/store.js +128 -128
- package/lib/topic-alias-recv.js +47 -0
- package/lib/topic-alias-send.js +93 -0
- package/lib/unique-message-id-provider.js +65 -0
- package/lib/validations.js +52 -52
- package/mqtt.js +21 -17
- package/package.json +124 -109
- package/types/index.d.ts +30 -28
- package/types/lib/client-options.d.ts +204 -191
- package/types/lib/client.d.ts +261 -240
- package/types/lib/connect/index.d.ts +10 -10
- package/types/lib/default-message-id-provider.d.ts +49 -0
- package/types/lib/message-id-provider.d.ts +40 -0
- package/types/lib/store-options.d.ts +6 -6
- package/types/lib/store.d.ts +46 -46
- package/types/lib/unique-message-id-provider.d.ts +48 -0
package/lib/connect/ali.js
CHANGED
|
@@ -1,128 +1,128 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/* global FileReader */
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
function buildProxy () {
|
|
13
|
-
|
|
14
|
-
proxy._write = function (chunk, encoding, next) {
|
|
15
|
-
my.sendSocketMessage({
|
|
16
|
-
data: chunk.buffer,
|
|
17
|
-
success: function () {
|
|
18
|
-
next()
|
|
19
|
-
},
|
|
20
|
-
fail: function () {
|
|
21
|
-
next(new Error())
|
|
22
|
-
}
|
|
23
|
-
})
|
|
24
|
-
}
|
|
25
|
-
proxy._flush = function socketEnd (done) {
|
|
26
|
-
my.closeSocket({
|
|
27
|
-
success: function () {
|
|
28
|
-
done()
|
|
29
|
-
}
|
|
30
|
-
})
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return proxy
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function setDefaultOpts (opts) {
|
|
37
|
-
if (!opts.hostname) {
|
|
38
|
-
opts.hostname = 'localhost'
|
|
39
|
-
}
|
|
40
|
-
if (!opts.path) {
|
|
41
|
-
opts.path = '/'
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (!opts.wsOptions) {
|
|
45
|
-
opts.wsOptions = {}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function buildUrl (opts, client) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
if (opts.port && opts.port !== 80 && opts.port !== 443) {
|
|
53
|
-
url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
|
54
|
-
}
|
|
55
|
-
if (typeof (opts.transformWsUrl) === 'function') {
|
|
56
|
-
url = opts.transformWsUrl(url, opts, client)
|
|
57
|
-
}
|
|
58
|
-
return url
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function bindEventHandler () {
|
|
62
|
-
if (isInitialized) return
|
|
63
|
-
|
|
64
|
-
isInitialized = true
|
|
65
|
-
|
|
66
|
-
my.onSocketOpen(function () {
|
|
67
|
-
stream.setReadable(proxy)
|
|
68
|
-
stream.setWritable(proxy)
|
|
69
|
-
stream.emit('connect')
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
my.onSocketMessage(function (res) {
|
|
73
|
-
if (typeof res.data === 'string') {
|
|
74
|
-
|
|
75
|
-
proxy.push(buffer)
|
|
76
|
-
} else {
|
|
77
|
-
|
|
78
|
-
reader.addEventListener('load', function () {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
82
|
-
else data = Buffer.from(data, 'utf8')
|
|
83
|
-
proxy.push(data)
|
|
84
|
-
})
|
|
85
|
-
reader.readAsArrayBuffer(res.data)
|
|
86
|
-
}
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
my.onSocketClose(function () {
|
|
90
|
-
stream.end()
|
|
91
|
-
stream.destroy()
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
my.onSocketError(function (res) {
|
|
95
|
-
stream.destroy(res)
|
|
96
|
-
})
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function buildStream (client, opts) {
|
|
100
|
-
opts.hostname = opts.hostname || opts.host
|
|
101
|
-
|
|
102
|
-
if (!opts.hostname) {
|
|
103
|
-
throw new Error('Could not determine host. Specify host manually.')
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
108
|
-
? 'mqttv3.1'
|
|
109
|
-
: 'mqtt'
|
|
110
|
-
|
|
111
|
-
setDefaultOpts(opts)
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
my = opts.my
|
|
115
|
-
my.connectSocket({
|
|
116
|
-
url: url,
|
|
117
|
-
protocols: websocketSubProtocol
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
proxy = buildProxy()
|
|
121
|
-
stream = duplexify.obj()
|
|
122
|
-
|
|
123
|
-
bindEventHandler()
|
|
124
|
-
|
|
125
|
-
return stream
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
module.exports = buildStream
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Transform = require('readable-stream').Transform
|
|
4
|
+
const duplexify = require('duplexify')
|
|
5
|
+
|
|
6
|
+
/* global FileReader */
|
|
7
|
+
let my
|
|
8
|
+
let proxy
|
|
9
|
+
let stream
|
|
10
|
+
let isInitialized = false
|
|
11
|
+
|
|
12
|
+
function buildProxy () {
|
|
13
|
+
const proxy = new Transform()
|
|
14
|
+
proxy._write = function (chunk, encoding, next) {
|
|
15
|
+
my.sendSocketMessage({
|
|
16
|
+
data: chunk.buffer,
|
|
17
|
+
success: function () {
|
|
18
|
+
next()
|
|
19
|
+
},
|
|
20
|
+
fail: function () {
|
|
21
|
+
next(new Error())
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
proxy._flush = function socketEnd (done) {
|
|
26
|
+
my.closeSocket({
|
|
27
|
+
success: function () {
|
|
28
|
+
done()
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return proxy
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function setDefaultOpts (opts) {
|
|
37
|
+
if (!opts.hostname) {
|
|
38
|
+
opts.hostname = 'localhost'
|
|
39
|
+
}
|
|
40
|
+
if (!opts.path) {
|
|
41
|
+
opts.path = '/'
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!opts.wsOptions) {
|
|
45
|
+
opts.wsOptions = {}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function buildUrl (opts, client) {
|
|
50
|
+
const protocol = opts.protocol === 'alis' ? 'wss' : 'ws'
|
|
51
|
+
let url = protocol + '://' + opts.hostname + opts.path
|
|
52
|
+
if (opts.port && opts.port !== 80 && opts.port !== 443) {
|
|
53
|
+
url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
|
54
|
+
}
|
|
55
|
+
if (typeof (opts.transformWsUrl) === 'function') {
|
|
56
|
+
url = opts.transformWsUrl(url, opts, client)
|
|
57
|
+
}
|
|
58
|
+
return url
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function bindEventHandler () {
|
|
62
|
+
if (isInitialized) return
|
|
63
|
+
|
|
64
|
+
isInitialized = true
|
|
65
|
+
|
|
66
|
+
my.onSocketOpen(function () {
|
|
67
|
+
stream.setReadable(proxy)
|
|
68
|
+
stream.setWritable(proxy)
|
|
69
|
+
stream.emit('connect')
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
my.onSocketMessage(function (res) {
|
|
73
|
+
if (typeof res.data === 'string') {
|
|
74
|
+
const buffer = Buffer.from(res.data, 'base64')
|
|
75
|
+
proxy.push(buffer)
|
|
76
|
+
} else {
|
|
77
|
+
const reader = new FileReader()
|
|
78
|
+
reader.addEventListener('load', function () {
|
|
79
|
+
let data = reader.result
|
|
80
|
+
|
|
81
|
+
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
82
|
+
else data = Buffer.from(data, 'utf8')
|
|
83
|
+
proxy.push(data)
|
|
84
|
+
})
|
|
85
|
+
reader.readAsArrayBuffer(res.data)
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
my.onSocketClose(function () {
|
|
90
|
+
stream.end()
|
|
91
|
+
stream.destroy()
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
my.onSocketError(function (res) {
|
|
95
|
+
stream.destroy(res)
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function buildStream (client, opts) {
|
|
100
|
+
opts.hostname = opts.hostname || opts.host
|
|
101
|
+
|
|
102
|
+
if (!opts.hostname) {
|
|
103
|
+
throw new Error('Could not determine host. Specify host manually.')
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const websocketSubProtocol =
|
|
107
|
+
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
108
|
+
? 'mqttv3.1'
|
|
109
|
+
: 'mqtt'
|
|
110
|
+
|
|
111
|
+
setDefaultOpts(opts)
|
|
112
|
+
|
|
113
|
+
const url = buildUrl(opts, client)
|
|
114
|
+
my = opts.my
|
|
115
|
+
my.connectSocket({
|
|
116
|
+
url: url,
|
|
117
|
+
protocols: websocketSubProtocol
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
proxy = buildProxy()
|
|
121
|
+
stream = duplexify.obj()
|
|
122
|
+
|
|
123
|
+
bindEventHandler()
|
|
124
|
+
|
|
125
|
+
return stream
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
module.exports = buildStream
|
package/lib/connect/index.js
CHANGED
|
@@ -1,164 +1,165 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
// eslint-disable-next-line camelcase
|
|
12
|
-
if ((typeof process !== 'undefined' && process.title !== 'browser') || typeof __webpack_require__ !== 'function') {
|
|
13
|
-
protocols.mqtt = require('./tcp')
|
|
14
|
-
protocols.tcp = require('./tcp')
|
|
15
|
-
protocols.ssl = require('./tls')
|
|
16
|
-
protocols.tls = require('./tls')
|
|
17
|
-
protocols.mqtts = require('./tls')
|
|
18
|
-
} else {
|
|
19
|
-
protocols.wx = require('./wx')
|
|
20
|
-
protocols.wxs = require('./wx')
|
|
21
|
-
|
|
22
|
-
protocols.ali = require('./ali')
|
|
23
|
-
protocols.alis = require('./ali')
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
protocols.ws = require('./ws')
|
|
27
|
-
protocols.wss = require('./ws')
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Parse the auth attribute and merge username and password in the options object.
|
|
31
|
-
*
|
|
32
|
-
* @param {Object} [opts] option object
|
|
33
|
-
*/
|
|
34
|
-
function parseAuthOptions (opts) {
|
|
35
|
-
|
|
36
|
-
if (opts.auth) {
|
|
37
|
-
matches = opts.auth.match(/^(.+):(.+)$/)
|
|
38
|
-
if (matches) {
|
|
39
|
-
opts.username = matches[1]
|
|
40
|
-
opts.password = matches[2]
|
|
41
|
-
} else {
|
|
42
|
-
opts.username = opts.auth
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* connect - connect to an MQTT broker.
|
|
49
|
-
*
|
|
50
|
-
* @param {String} [brokerUrl] - url of the broker, optional
|
|
51
|
-
* @param {Object} opts - see MqttClient#constructor
|
|
52
|
-
*/
|
|
53
|
-
function connect (brokerUrl, opts) {
|
|
54
|
-
debug('connecting to an MQTT broker...')
|
|
55
|
-
if ((typeof brokerUrl === 'object') && !opts) {
|
|
56
|
-
opts = brokerUrl
|
|
57
|
-
brokerUrl = null
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
opts = opts || {}
|
|
61
|
-
|
|
62
|
-
if (brokerUrl) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
opts.protocol
|
|
114
|
-
|
|
115
|
-
'
|
|
116
|
-
'
|
|
117
|
-
'
|
|
118
|
-
'
|
|
119
|
-
'
|
|
120
|
-
'
|
|
121
|
-
'
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
opts.
|
|
147
|
-
opts.
|
|
148
|
-
opts.
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
module.exports
|
|
163
|
-
module.exports.
|
|
164
|
-
module.exports.
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const MqttClient = require('../client')
|
|
4
|
+
const Store = require('../store')
|
|
5
|
+
const url = require('url')
|
|
6
|
+
const xtend = require('xtend')
|
|
7
|
+
const debug = require('debug')('mqttjs')
|
|
8
|
+
|
|
9
|
+
const protocols = {}
|
|
10
|
+
|
|
11
|
+
// eslint-disable-next-line camelcase
|
|
12
|
+
if ((typeof process !== 'undefined' && process.title !== 'browser') || typeof __webpack_require__ !== 'function') {
|
|
13
|
+
protocols.mqtt = require('./tcp')
|
|
14
|
+
protocols.tcp = require('./tcp')
|
|
15
|
+
protocols.ssl = require('./tls')
|
|
16
|
+
protocols.tls = require('./tls')
|
|
17
|
+
protocols.mqtts = require('./tls')
|
|
18
|
+
} else {
|
|
19
|
+
protocols.wx = require('./wx')
|
|
20
|
+
protocols.wxs = require('./wx')
|
|
21
|
+
|
|
22
|
+
protocols.ali = require('./ali')
|
|
23
|
+
protocols.alis = require('./ali')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
protocols.ws = require('./ws')
|
|
27
|
+
protocols.wss = require('./ws')
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Parse the auth attribute and merge username and password in the options object.
|
|
31
|
+
*
|
|
32
|
+
* @param {Object} [opts] option object
|
|
33
|
+
*/
|
|
34
|
+
function parseAuthOptions (opts) {
|
|
35
|
+
let matches
|
|
36
|
+
if (opts.auth) {
|
|
37
|
+
matches = opts.auth.match(/^(.+):(.+)$/)
|
|
38
|
+
if (matches) {
|
|
39
|
+
opts.username = matches[1]
|
|
40
|
+
opts.password = matches[2]
|
|
41
|
+
} else {
|
|
42
|
+
opts.username = opts.auth
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* connect - connect to an MQTT broker.
|
|
49
|
+
*
|
|
50
|
+
* @param {String} [brokerUrl] - url of the broker, optional
|
|
51
|
+
* @param {Object} opts - see MqttClient#constructor
|
|
52
|
+
*/
|
|
53
|
+
function connect (brokerUrl, opts) {
|
|
54
|
+
debug('connecting to an MQTT broker...')
|
|
55
|
+
if ((typeof brokerUrl === 'object') && !opts) {
|
|
56
|
+
opts = brokerUrl
|
|
57
|
+
brokerUrl = null
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
opts = opts || {}
|
|
61
|
+
|
|
62
|
+
if (brokerUrl) {
|
|
63
|
+
// eslint-disable-next-line
|
|
64
|
+
const parsed = url.parse(brokerUrl, true)
|
|
65
|
+
if (parsed.port != null) {
|
|
66
|
+
parsed.port = Number(parsed.port)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
opts = xtend(parsed, opts)
|
|
70
|
+
|
|
71
|
+
if (opts.protocol === null) {
|
|
72
|
+
throw new Error('Missing protocol')
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
opts.protocol = opts.protocol.replace(/:$/, '')
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// merge in the auth options if supplied
|
|
79
|
+
parseAuthOptions(opts)
|
|
80
|
+
|
|
81
|
+
// support clientId passed in the query string of the url
|
|
82
|
+
if (opts.query && typeof opts.query.clientId === 'string') {
|
|
83
|
+
opts.clientId = opts.query.clientId
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (opts.cert && opts.key) {
|
|
87
|
+
if (opts.protocol) {
|
|
88
|
+
if (['mqtts', 'wss', 'wxs', 'alis'].indexOf(opts.protocol) === -1) {
|
|
89
|
+
switch (opts.protocol) {
|
|
90
|
+
case 'mqtt':
|
|
91
|
+
opts.protocol = 'mqtts'
|
|
92
|
+
break
|
|
93
|
+
case 'ws':
|
|
94
|
+
opts.protocol = 'wss'
|
|
95
|
+
break
|
|
96
|
+
case 'wx':
|
|
97
|
+
opts.protocol = 'wxs'
|
|
98
|
+
break
|
|
99
|
+
case 'ali':
|
|
100
|
+
opts.protocol = 'alis'
|
|
101
|
+
break
|
|
102
|
+
default:
|
|
103
|
+
throw new Error('Unknown protocol for secure connection: "' + opts.protocol + '"!')
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
// A cert and key was provided, however no protocol was specified, so we will throw an error.
|
|
108
|
+
throw new Error('Missing secure protocol key')
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (!protocols[opts.protocol]) {
|
|
113
|
+
const isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1
|
|
114
|
+
opts.protocol = [
|
|
115
|
+
'mqtt',
|
|
116
|
+
'mqtts',
|
|
117
|
+
'ws',
|
|
118
|
+
'wss',
|
|
119
|
+
'wx',
|
|
120
|
+
'wxs',
|
|
121
|
+
'ali',
|
|
122
|
+
'alis'
|
|
123
|
+
].filter(function (key, index) {
|
|
124
|
+
if (isSecure && index % 2 === 0) {
|
|
125
|
+
// Skip insecure protocols when requesting a secure one.
|
|
126
|
+
return false
|
|
127
|
+
}
|
|
128
|
+
return (typeof protocols[key] === 'function')
|
|
129
|
+
})[0]
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (opts.clean === false && !opts.clientId) {
|
|
133
|
+
throw new Error('Missing clientId for unclean clients')
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (opts.protocol) {
|
|
137
|
+
opts.defaultProtocol = opts.protocol
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function wrapper (client) {
|
|
141
|
+
if (opts.servers) {
|
|
142
|
+
if (!client._reconnectCount || client._reconnectCount === opts.servers.length) {
|
|
143
|
+
client._reconnectCount = 0
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
opts.host = opts.servers[client._reconnectCount].host
|
|
147
|
+
opts.port = opts.servers[client._reconnectCount].port
|
|
148
|
+
opts.protocol = (!opts.servers[client._reconnectCount].protocol ? opts.defaultProtocol : opts.servers[client._reconnectCount].protocol)
|
|
149
|
+
opts.hostname = opts.host
|
|
150
|
+
|
|
151
|
+
client._reconnectCount++
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
debug('calling streambuilder for', opts.protocol)
|
|
155
|
+
return protocols[opts.protocol](client, opts)
|
|
156
|
+
}
|
|
157
|
+
const client = new MqttClient(wrapper, opts)
|
|
158
|
+
client.on('error', function () { /* Automatically set up client error handling */ })
|
|
159
|
+
return client
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
module.exports = connect
|
|
163
|
+
module.exports.connect = connect
|
|
164
|
+
module.exports.MqttClient = MqttClient
|
|
165
|
+
module.exports.Store = Store
|
package/lib/connect/tcp.js
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/*
|
|
6
|
-
variables port and host can be removed since
|
|
7
|
-
you have all required information in opts object
|
|
8
|
-
*/
|
|
9
|
-
function streamBuilder (client, opts) {
|
|
10
|
-
|
|
11
|
-
opts.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
module.exports = streamBuilder
|
|
1
|
+
'use strict'
|
|
2
|
+
const net = require('net')
|
|
3
|
+
const debug = require('debug')('mqttjs:tcp')
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
variables port and host can be removed since
|
|
7
|
+
you have all required information in opts object
|
|
8
|
+
*/
|
|
9
|
+
function streamBuilder (client, opts) {
|
|
10
|
+
opts.port = opts.port || 1883
|
|
11
|
+
opts.hostname = opts.hostname || opts.host || 'localhost'
|
|
12
|
+
|
|
13
|
+
const port = opts.port
|
|
14
|
+
const host = opts.hostname
|
|
15
|
+
|
|
16
|
+
debug('port %d and host %s', port, host)
|
|
17
|
+
return net.createConnection(port, host)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = streamBuilder
|