mqtt 2.18.5 → 2.18.9
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/CONTRIBUTING.md +27 -27
- package/LICENSE.md +15 -15
- package/README.md +612 -612
- package/bin/pub.js +146 -146
- package/bin/sub.js +123 -123
- package/dist/mqtt.js +14275 -10729
- 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/examples/client/secure-client.js +24 -24
- package/examples/client/simple-both.js +13 -13
- package/examples/client/simple-publish.js +7 -7
- package/examples/client/simple-subscribe.js +9 -9
- package/examples/tls client/crt.ca.cg.pem +34 -34
- package/examples/tls client/mqttclient.js +48 -48
- package/examples/tls client/tls-cert.pem +13 -13
- package/examples/tls client/tls-key.pem +15 -15
- package/examples/wss/client.js +49 -49
- package/examples/wss/client_with_proxy.js +58 -58
- package/lib/client.js +1129 -1102
- package/lib/connect/index.js +149 -149
- package/lib/connect/tcp.js +19 -19
- package/lib/connect/tls.js +41 -41
- package/lib/connect/ws.js +92 -92
- package/lib/connect/wx.js +110 -110
- package/lib/store.js +140 -140
- package/lib/validations.js +52 -52
- package/mqtt.js +41 -41
- package/package.json +107 -111
- package/test/abstract_client.js +2667 -2533
- package/test/abstract_store.js +135 -133
- package/test/browser/server.js +132 -132
- package/test/browser/test.js +92 -92
- package/test/browser/wx.js +93 -93
- package/test/client.js +544 -544
- package/test/helpers/private-csr.pem +16 -16
- package/test/helpers/private-key.pem +27 -27
- package/test/helpers/public-cert.pem +19 -19
- package/test/helpers/public-key.pem +9 -9
- package/test/helpers/server.js +52 -52
- package/test/helpers/server_process.js +9 -9
- package/test/helpers/tls-cert.pem +14 -14
- package/test/helpers/tls-csr.pem +11 -11
- package/test/helpers/tls-key.pem +15 -15
- package/test/helpers/wrong-cert.pem +13 -13
- package/test/helpers/wrong-csr.pem +11 -11
- package/test/helpers/wrong-key.pem +15 -15
- package/test/mocha.opts +4 -4
- package/test/mqtt.js +230 -230
- package/test/mqtt_store.js +9 -9
- package/test/secure_client.js +157 -157
- package/test/server.js +65 -65
- package/test/store.js +10 -10
- package/test/typescript/broker-connect-subscribe-and-publish.ts +22 -22
- package/test/typescript/tsconfig.json +14 -14
- package/test/util.js +13 -13
- package/test/websocket_client.js +144 -144
- package/types/index.d.ts +27 -27
- package/types/lib/client-options.d.ts +137 -137
- package/types/lib/client.d.ts +196 -196
- package/types/lib/connect/index.d.ts +10 -10
- package/types/lib/store-options.d.ts +6 -6
- package/types/lib/store.d.ts +46 -46
- package/test/typescript/broker-connect-subscribe-and-publish.js +0 -24
- package/test/typescript/broker-connect-subscribe-and-publish.js.map +0 -1
package/bin/pub.js
CHANGED
|
@@ -1,146 +1,146 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
'use strict'
|
|
4
|
-
|
|
5
|
-
var mqtt = require('../')
|
|
6
|
-
var pump = require('pump')
|
|
7
|
-
var path = require('path')
|
|
8
|
-
var fs = require('fs')
|
|
9
|
-
var concat = require('concat-stream')
|
|
10
|
-
var Writable = require('readable-stream').Writable
|
|
11
|
-
var helpMe = require('help-me')({
|
|
12
|
-
dir: path.join(__dirname, '..', 'doc')
|
|
13
|
-
})
|
|
14
|
-
var minimist = require('minimist')
|
|
15
|
-
var split2 = require('split2')
|
|
16
|
-
|
|
17
|
-
function send (args) {
|
|
18
|
-
var client = mqtt.connect(args)
|
|
19
|
-
client.on('connect', function () {
|
|
20
|
-
client.publish(args.topic, args.message, args, function (err) {
|
|
21
|
-
if (err) {
|
|
22
|
-
console.warn(err)
|
|
23
|
-
}
|
|
24
|
-
client.end()
|
|
25
|
-
})
|
|
26
|
-
})
|
|
27
|
-
client.on('error', function (err) {
|
|
28
|
-
console.warn(err)
|
|
29
|
-
client.end()
|
|
30
|
-
})
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function multisend (args) {
|
|
34
|
-
var client = mqtt.connect(args)
|
|
35
|
-
var sender = new Writable({
|
|
36
|
-
objectMode: true
|
|
37
|
-
})
|
|
38
|
-
sender._write = function (line, enc, cb) {
|
|
39
|
-
client.publish(args.topic, line.trim(), args, cb)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
client.on('connect', function () {
|
|
43
|
-
pump(process.stdin, split2(), sender, function (err) {
|
|
44
|
-
client.end()
|
|
45
|
-
if (err) {
|
|
46
|
-
throw err
|
|
47
|
-
}
|
|
48
|
-
})
|
|
49
|
-
})
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function start (args) {
|
|
53
|
-
args = minimist(args, {
|
|
54
|
-
string: ['hostname', 'username', 'password', 'key', 'cert', 'ca', 'message', 'clientId', 'i', 'id'],
|
|
55
|
-
boolean: ['stdin', 'retain', 'help', 'insecure', 'multiline'],
|
|
56
|
-
alias: {
|
|
57
|
-
port: 'p',
|
|
58
|
-
hostname: ['h', 'host'],
|
|
59
|
-
topic: 't',
|
|
60
|
-
message: 'm',
|
|
61
|
-
qos: 'q',
|
|
62
|
-
clientId: ['i', 'id'],
|
|
63
|
-
retain: 'r',
|
|
64
|
-
username: 'u',
|
|
65
|
-
password: 'P',
|
|
66
|
-
stdin: 's',
|
|
67
|
-
multiline: 'M',
|
|
68
|
-
protocol: ['C', 'l'],
|
|
69
|
-
help: 'H',
|
|
70
|
-
ca: 'cafile'
|
|
71
|
-
},
|
|
72
|
-
default: {
|
|
73
|
-
host: 'localhost',
|
|
74
|
-
qos: 0,
|
|
75
|
-
retain: false,
|
|
76
|
-
topic: '',
|
|
77
|
-
message: ''
|
|
78
|
-
}
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
if (args.help) {
|
|
82
|
-
return helpMe.toStdout('publish')
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (args.key) {
|
|
86
|
-
args.key = fs.readFileSync(args.key)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (args.cert) {
|
|
90
|
-
args.cert = fs.readFileSync(args.cert)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (args.ca) {
|
|
94
|
-
args.ca = fs.readFileSync(args.ca)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (args.key && args.cert && !args.protocol) {
|
|
98
|
-
args.protocol = 'mqtts'
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (args.port) {
|
|
102
|
-
if (typeof args.port !== 'number') {
|
|
103
|
-
console.warn('# Port: number expected, \'%s\' was given.', typeof args.port)
|
|
104
|
-
return
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (args['will-topic']) {
|
|
109
|
-
args.will = {}
|
|
110
|
-
args.will.topic = args['will-topic']
|
|
111
|
-
args.will.payload = args['will-message']
|
|
112
|
-
args.will.qos = args['will-qos']
|
|
113
|
-
args.will.retain = args['will-retain']
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (args.insecure) {
|
|
117
|
-
args.rejectUnauthorized = false
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
args.topic = (args.topic || args._.shift()).toString()
|
|
121
|
-
args.message = (args.message || args._.shift()).toString()
|
|
122
|
-
|
|
123
|
-
if (!args.topic) {
|
|
124
|
-
console.error('missing topic\n')
|
|
125
|
-
return helpMe.toStdout('publish')
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (args.stdin) {
|
|
129
|
-
if (args.multiline) {
|
|
130
|
-
multisend(args)
|
|
131
|
-
} else {
|
|
132
|
-
process.stdin.pipe(concat(function (data) {
|
|
133
|
-
args.message = data
|
|
134
|
-
send(args)
|
|
135
|
-
}))
|
|
136
|
-
}
|
|
137
|
-
} else {
|
|
138
|
-
send(args)
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
module.exports = start
|
|
143
|
-
|
|
144
|
-
if (require.main === module) {
|
|
145
|
-
start(process.argv.slice(2))
|
|
146
|
-
}
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
'use strict'
|
|
4
|
+
|
|
5
|
+
var mqtt = require('../')
|
|
6
|
+
var pump = require('pump')
|
|
7
|
+
var path = require('path')
|
|
8
|
+
var fs = require('fs')
|
|
9
|
+
var concat = require('concat-stream')
|
|
10
|
+
var Writable = require('readable-stream').Writable
|
|
11
|
+
var helpMe = require('help-me')({
|
|
12
|
+
dir: path.join(__dirname, '..', 'doc')
|
|
13
|
+
})
|
|
14
|
+
var minimist = require('minimist')
|
|
15
|
+
var split2 = require('split2')
|
|
16
|
+
|
|
17
|
+
function send (args) {
|
|
18
|
+
var client = mqtt.connect(args)
|
|
19
|
+
client.on('connect', function () {
|
|
20
|
+
client.publish(args.topic, args.message, args, function (err) {
|
|
21
|
+
if (err) {
|
|
22
|
+
console.warn(err)
|
|
23
|
+
}
|
|
24
|
+
client.end()
|
|
25
|
+
})
|
|
26
|
+
})
|
|
27
|
+
client.on('error', function (err) {
|
|
28
|
+
console.warn(err)
|
|
29
|
+
client.end()
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function multisend (args) {
|
|
34
|
+
var client = mqtt.connect(args)
|
|
35
|
+
var sender = new Writable({
|
|
36
|
+
objectMode: true
|
|
37
|
+
})
|
|
38
|
+
sender._write = function (line, enc, cb) {
|
|
39
|
+
client.publish(args.topic, line.trim(), args, cb)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
client.on('connect', function () {
|
|
43
|
+
pump(process.stdin, split2(), sender, function (err) {
|
|
44
|
+
client.end()
|
|
45
|
+
if (err) {
|
|
46
|
+
throw err
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function start (args) {
|
|
53
|
+
args = minimist(args, {
|
|
54
|
+
string: ['hostname', 'username', 'password', 'key', 'cert', 'ca', 'message', 'clientId', 'i', 'id'],
|
|
55
|
+
boolean: ['stdin', 'retain', 'help', 'insecure', 'multiline'],
|
|
56
|
+
alias: {
|
|
57
|
+
port: 'p',
|
|
58
|
+
hostname: ['h', 'host'],
|
|
59
|
+
topic: 't',
|
|
60
|
+
message: 'm',
|
|
61
|
+
qos: 'q',
|
|
62
|
+
clientId: ['i', 'id'],
|
|
63
|
+
retain: 'r',
|
|
64
|
+
username: 'u',
|
|
65
|
+
password: 'P',
|
|
66
|
+
stdin: 's',
|
|
67
|
+
multiline: 'M',
|
|
68
|
+
protocol: ['C', 'l'],
|
|
69
|
+
help: 'H',
|
|
70
|
+
ca: 'cafile'
|
|
71
|
+
},
|
|
72
|
+
default: {
|
|
73
|
+
host: 'localhost',
|
|
74
|
+
qos: 0,
|
|
75
|
+
retain: false,
|
|
76
|
+
topic: '',
|
|
77
|
+
message: ''
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
if (args.help) {
|
|
82
|
+
return helpMe.toStdout('publish')
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (args.key) {
|
|
86
|
+
args.key = fs.readFileSync(args.key)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (args.cert) {
|
|
90
|
+
args.cert = fs.readFileSync(args.cert)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (args.ca) {
|
|
94
|
+
args.ca = fs.readFileSync(args.ca)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (args.key && args.cert && !args.protocol) {
|
|
98
|
+
args.protocol = 'mqtts'
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (args.port) {
|
|
102
|
+
if (typeof args.port !== 'number') {
|
|
103
|
+
console.warn('# Port: number expected, \'%s\' was given.', typeof args.port)
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (args['will-topic']) {
|
|
109
|
+
args.will = {}
|
|
110
|
+
args.will.topic = args['will-topic']
|
|
111
|
+
args.will.payload = args['will-message']
|
|
112
|
+
args.will.qos = args['will-qos']
|
|
113
|
+
args.will.retain = args['will-retain']
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (args.insecure) {
|
|
117
|
+
args.rejectUnauthorized = false
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
args.topic = (args.topic || args._.shift()).toString()
|
|
121
|
+
args.message = (args.message || args._.shift()).toString()
|
|
122
|
+
|
|
123
|
+
if (!args.topic) {
|
|
124
|
+
console.error('missing topic\n')
|
|
125
|
+
return helpMe.toStdout('publish')
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (args.stdin) {
|
|
129
|
+
if (args.multiline) {
|
|
130
|
+
multisend(args)
|
|
131
|
+
} else {
|
|
132
|
+
process.stdin.pipe(concat(function (data) {
|
|
133
|
+
args.message = data
|
|
134
|
+
send(args)
|
|
135
|
+
}))
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
send(args)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
module.exports = start
|
|
143
|
+
|
|
144
|
+
if (require.main === module) {
|
|
145
|
+
start(process.argv.slice(2))
|
|
146
|
+
}
|
package/bin/sub.js
CHANGED
|
@@ -1,123 +1,123 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
var mqtt = require('../')
|
|
4
|
-
var path = require('path')
|
|
5
|
-
var fs = require('fs')
|
|
6
|
-
var helpMe = require('help-me')({
|
|
7
|
-
dir: path.join(__dirname, '..', 'doc')
|
|
8
|
-
})
|
|
9
|
-
var minimist = require('minimist')
|
|
10
|
-
|
|
11
|
-
function start (args) {
|
|
12
|
-
args = minimist(args, {
|
|
13
|
-
string: ['hostname', 'username', 'password', 'key', 'cert', 'ca', 'clientId', 'i', 'id'],
|
|
14
|
-
boolean: ['stdin', 'help', 'clean', 'insecure'],
|
|
15
|
-
alias: {
|
|
16
|
-
port: 'p',
|
|
17
|
-
hostname: ['h', 'host'],
|
|
18
|
-
topic: 't',
|
|
19
|
-
qos: 'q',
|
|
20
|
-
clean: 'c',
|
|
21
|
-
keepalive: 'k',
|
|
22
|
-
clientId: ['i', 'id'],
|
|
23
|
-
username: 'u',
|
|
24
|
-
password: 'P',
|
|
25
|
-
protocol: ['C', 'l'],
|
|
26
|
-
verbose: 'v',
|
|
27
|
-
help: '-H',
|
|
28
|
-
ca: 'cafile'
|
|
29
|
-
},
|
|
30
|
-
default: {
|
|
31
|
-
host: 'localhost',
|
|
32
|
-
qos: 0,
|
|
33
|
-
retain: false,
|
|
34
|
-
clean: true,
|
|
35
|
-
keepAlive: 30 // 30 sec
|
|
36
|
-
}
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
if (args.help) {
|
|
40
|
-
return helpMe.toStdout('subscribe')
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
args.topic = args.topic || args._.shift()
|
|
44
|
-
|
|
45
|
-
if (!args.topic) {
|
|
46
|
-
console.error('missing topic\n')
|
|
47
|
-
return helpMe.toStdout('subscribe')
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (args.key) {
|
|
51
|
-
args.key = fs.readFileSync(args.key)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (args.cert) {
|
|
55
|
-
args.cert = fs.readFileSync(args.cert)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (args.ca) {
|
|
59
|
-
args.ca = fs.readFileSync(args.ca)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (args.key && args.cert && !args.protocol) {
|
|
63
|
-
args.protocol = 'mqtts'
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (args.insecure) {
|
|
67
|
-
args.rejectUnauthorized = false
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (args.port) {
|
|
71
|
-
if (typeof args.port !== 'number') {
|
|
72
|
-
console.warn('# Port: number expected, \'%s\' was given.', typeof args.port)
|
|
73
|
-
return
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (args['will-topic']) {
|
|
78
|
-
args.will = {}
|
|
79
|
-
args.will.topic = args['will-topic']
|
|
80
|
-
args.will.payload = args['will-message']
|
|
81
|
-
args.will.qos = args['will-qos']
|
|
82
|
-
args.will.retain = args['will-retain']
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
args.keepAlive = args['keep-alive']
|
|
86
|
-
|
|
87
|
-
var client = mqtt.connect(args)
|
|
88
|
-
|
|
89
|
-
client.on('connect', function () {
|
|
90
|
-
client.subscribe(args.topic, { qos: args.qos }, function (err, result) {
|
|
91
|
-
if (err) {
|
|
92
|
-
console.error(err)
|
|
93
|
-
process.exit(1)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
result.forEach(function (sub) {
|
|
97
|
-
if (sub.qos > 2) {
|
|
98
|
-
console.error('subscription negated to', sub.topic, 'with code', sub.qos)
|
|
99
|
-
process.exit(1)
|
|
100
|
-
}
|
|
101
|
-
})
|
|
102
|
-
})
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
client.on('message', function (topic, payload) {
|
|
106
|
-
if (args.verbose) {
|
|
107
|
-
console.log(topic, payload.toString())
|
|
108
|
-
} else {
|
|
109
|
-
console.log(payload.toString())
|
|
110
|
-
}
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
client.on('error', function (err) {
|
|
114
|
-
console.warn(err)
|
|
115
|
-
client.end()
|
|
116
|
-
})
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
module.exports = start
|
|
120
|
-
|
|
121
|
-
if (require.main === module) {
|
|
122
|
-
start(process.argv.slice(2))
|
|
123
|
-
}
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
var mqtt = require('../')
|
|
4
|
+
var path = require('path')
|
|
5
|
+
var fs = require('fs')
|
|
6
|
+
var helpMe = require('help-me')({
|
|
7
|
+
dir: path.join(__dirname, '..', 'doc')
|
|
8
|
+
})
|
|
9
|
+
var minimist = require('minimist')
|
|
10
|
+
|
|
11
|
+
function start (args) {
|
|
12
|
+
args = minimist(args, {
|
|
13
|
+
string: ['hostname', 'username', 'password', 'key', 'cert', 'ca', 'clientId', 'i', 'id'],
|
|
14
|
+
boolean: ['stdin', 'help', 'clean', 'insecure'],
|
|
15
|
+
alias: {
|
|
16
|
+
port: 'p',
|
|
17
|
+
hostname: ['h', 'host'],
|
|
18
|
+
topic: 't',
|
|
19
|
+
qos: 'q',
|
|
20
|
+
clean: 'c',
|
|
21
|
+
keepalive: 'k',
|
|
22
|
+
clientId: ['i', 'id'],
|
|
23
|
+
username: 'u',
|
|
24
|
+
password: 'P',
|
|
25
|
+
protocol: ['C', 'l'],
|
|
26
|
+
verbose: 'v',
|
|
27
|
+
help: '-H',
|
|
28
|
+
ca: 'cafile'
|
|
29
|
+
},
|
|
30
|
+
default: {
|
|
31
|
+
host: 'localhost',
|
|
32
|
+
qos: 0,
|
|
33
|
+
retain: false,
|
|
34
|
+
clean: true,
|
|
35
|
+
keepAlive: 30 // 30 sec
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
if (args.help) {
|
|
40
|
+
return helpMe.toStdout('subscribe')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
args.topic = args.topic || args._.shift()
|
|
44
|
+
|
|
45
|
+
if (!args.topic) {
|
|
46
|
+
console.error('missing topic\n')
|
|
47
|
+
return helpMe.toStdout('subscribe')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (args.key) {
|
|
51
|
+
args.key = fs.readFileSync(args.key)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (args.cert) {
|
|
55
|
+
args.cert = fs.readFileSync(args.cert)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (args.ca) {
|
|
59
|
+
args.ca = fs.readFileSync(args.ca)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (args.key && args.cert && !args.protocol) {
|
|
63
|
+
args.protocol = 'mqtts'
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (args.insecure) {
|
|
67
|
+
args.rejectUnauthorized = false
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (args.port) {
|
|
71
|
+
if (typeof args.port !== 'number') {
|
|
72
|
+
console.warn('# Port: number expected, \'%s\' was given.', typeof args.port)
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (args['will-topic']) {
|
|
78
|
+
args.will = {}
|
|
79
|
+
args.will.topic = args['will-topic']
|
|
80
|
+
args.will.payload = args['will-message']
|
|
81
|
+
args.will.qos = args['will-qos']
|
|
82
|
+
args.will.retain = args['will-retain']
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
args.keepAlive = args['keep-alive']
|
|
86
|
+
|
|
87
|
+
var client = mqtt.connect(args)
|
|
88
|
+
|
|
89
|
+
client.on('connect', function () {
|
|
90
|
+
client.subscribe(args.topic, { qos: args.qos }, function (err, result) {
|
|
91
|
+
if (err) {
|
|
92
|
+
console.error(err)
|
|
93
|
+
process.exit(1)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
result.forEach(function (sub) {
|
|
97
|
+
if (sub.qos > 2) {
|
|
98
|
+
console.error('subscription negated to', sub.topic, 'with code', sub.qos)
|
|
99
|
+
process.exit(1)
|
|
100
|
+
}
|
|
101
|
+
})
|
|
102
|
+
})
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
client.on('message', function (topic, payload) {
|
|
106
|
+
if (args.verbose) {
|
|
107
|
+
console.log(topic, payload.toString())
|
|
108
|
+
} else {
|
|
109
|
+
console.log(payload.toString())
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
client.on('error', function (err) {
|
|
114
|
+
console.warn(err)
|
|
115
|
+
client.end()
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
module.exports = start
|
|
120
|
+
|
|
121
|
+
if (require.main === module) {
|
|
122
|
+
start(process.argv.slice(2))
|
|
123
|
+
}
|