Skip to content

Commit

Permalink
feat(cli/sub): update sub options table
Browse files Browse the repository at this point in the history
  • Loading branch information
ysfscream committed Jul 28, 2022
1 parent 7c8284a commit 8fdc320
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
3 changes: 3 additions & 0 deletions cli/README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ mqttx sub --help
| -u, --username <USER> | 连接到 MQTT Broker 的用户名 |
| -P, --password <PASS> | 连接到 MQTT Broker 的密码 |
| -l, --protocol <PROTO> | 连接时的协议,mqtt, mqtts, ws or wss |
| -nl, --no_local | MQTT 5.0 订阅选项中的 no local 标识 |
| -rap, --retain-as-published | MQTT 5.0 订阅选项中的 retain as published 标识 |
| -rh, --retain-handling <0/1/2> | MQTT 5.0 订阅选项中的 retain handling 标识 |
| --key <PATH> | key 文件的路径 |
| --cert <PATH> | cert 文件的路径 |
| --ca | ca 证书的文件路径 |
Expand Down
7 changes: 5 additions & 2 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,20 @@ mqttx sub --help
| -p, --port <PORT> | the broker port |
| -i, --client-id <ID> | the client id |
| -q, --qos <0/1/2> | the QoS of the message (default: 0) |
| --no-clean | set the clean session flag to false (default: true) |
| --no-clean | set the clean session flag to false (default: true) |
| -t, --topic <TOPIC> | the message topic |
| -k, --keepalive <SEC> | send a ping every SEC seconds (default: 30) |
| -u, --username <USER> | the username |
| -P, --password <PASS> | the password |
| -l, --protocol <PROTO> | the protocol to use, mqtt, mqtts, ws or wss |
| -nl, --no_local | the no local MQTT 5.0 flag |
| -rap, --retain-as-published | the retain as published MQTT 5.0 flag |
| -rh, --retain-handling <0/1/2> | the retain handling MQTT 5.0 |
| --key <PATH> | path to the key file |
| --cert <PATH> | path to the cert file |
| --ca | path to the ca certificate |
| --insecure | do not verify the server certificate |
| -up, --user-properties <USERPROPERTIES...> | the user properties of MQTT 5.0 (e.g. -up "name: mqttx cli") |
| -up, --user-properties <USERPROPERTIES...> | the user properties of MQTT 5.0 (e.g. -up "name: mqttx cli") |
| --will-topic <TOPIC> | the will topic |
| --will-message <BODY> | the will message |
| --will-qos <0/1/2> | the will qos |
Expand Down
7 changes: 3 additions & 4 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,13 @@ export class Commander {
.option('-u, --username <USER>', 'the username')
.option('-P, --password <PASS>', 'the password')
.option('-l, --protocol <PROTO>', 'the protocol to use, mqtt, mqtts, ws or wss', parseProtocol)
.option('-nl, --no_local', 'the no local MQTT 5.0 flag')
.option('-rap, --retain-as-published', 'the retain as published MQTT 5.0 flag')
.option('-rh, --retain-handling <0/1/2>', 'the retain handling MQTT 5.0', parseNumber)
.option('--key <PATH>', 'path to the key file')
.option('--cert <PATH>', 'path to the cert file')
.option('--ca <PATH>', 'path to the ca certificate')
.option('--insecure', 'do not verify the server certificate')
.option('--asd', 'do not verify the server certificate')
.option('-nl, --no_local', 'the no local MQTT 5.0 flag')
.option('-rap, --retain-as-published', 'the retain as published MQTT 5.0 flag')
.option('-rh, --retain-handling <0/1/2>', 'the retain handling MQTT 5.0', parseNumber)
.option(
'-up, --user-properties <USERPROPERTIES...>',
'the user properties of MQTT 5.0 (e.g. -up "name: mqttx cli")',
Expand Down
43 changes: 24 additions & 19 deletions cli/src/lib/sub.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as mqtt from 'mqtt'
import * as fs from 'fs'
import { IClientSubscribeOptions } from 'mqtt'

const sub = (options: any) => {
options.protocolVersion = options.mqttVersion
Expand Down Expand Up @@ -45,27 +46,31 @@ const sub = (options: any) => {

client.on('connect', () => {
const { topic, qos, no_local, retainAsPublished, retainHandling } = options
client.subscribe(
topic,
{
qos,
nl: no_local,
rap: retainAsPublished,
rh: retainHandling,
},
(err, result) => {
if (err) {
console.error(err)
const subOptions: {
qos: number
nl?: boolean
rap?: boolean
rh?: number
} = {
qos,
}
if (options.protocolVersion === 5) {
subOptions.nl = no_local
subOptions.rap = retainAsPublished
subOptions.rh = retainHandling
}
client.subscribe(topic, subOptions as IClientSubscribeOptions, (err, result) => {
if (err) {
console.error(err)
process.exit(1)
}
result.forEach((sub) => {
if (sub.qos > 2) {
console.error('subscription negated to', sub.topic, 'with code', sub.qos)
process.exit(1)
}
result.forEach((sub) => {
if (sub.qos > 2) {
console.error('subscription negated to', sub.topic, 'with code', sub.qos)
process.exit(1)
}
})
},
)
})
})
})

client.on('message', (topic, payload, packet) => {
Expand Down

0 comments on commit 8fdc320

Please sign in to comment.