Skip to content

Commit

Permalink
feat(cli): supports ws and wss protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
Red-Asuka committed May 25, 2023
1 parent a81f4d4 commit d174f14
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
7 changes: 7 additions & 0 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class Commander {
.option('-u, --username <USER>', 'the username')
.option('-P, --password <PASS>', 'the password')
.option('-l, --protocol <PROTO>', 'the protocol to use, mqtt or mqtts', parseProtocol, 'mqtt')
.option('--path <PATH>', 'the path of websocket', '/mqtt')
.option('--key <PATH>', 'path to the key file')
.option('--cert <PATH>', 'path to the cert file')
.option('--ca <PATH>', 'path to the ca certificate')
Expand Down Expand Up @@ -142,6 +143,7 @@ export class Commander {
.option('-u, --username <USER>', 'the username')
.option('-P, --password <PASS>', 'the password')
.option('-l, --protocol <PROTO>', 'the protocol to use, mqtt or mqtts', parseProtocol, 'mqtt')
.option('--path <PATH>', 'the path of websocket', '/mqtt')
.option('--key <PATH>', 'path to the key file')
.option('--cert <PATH>', 'path to the cert file')
.option('--ca <PATH>', 'path to the ca certificate')
Expand Down Expand Up @@ -229,6 +231,7 @@ export class Commander {
.option('-u, --username <USER>', 'the username')
.option('-P, --password <PASS>', 'the password')
.option('-l, --protocol <PROTO>', 'the protocol to use, mqtt or mqtts', parseProtocol, 'mqtt')
.option('--path <PATH>', 'the path of websocket', '/mqtt')
.option('--key <PATH>', 'path to the key file')
.option('--cert <PATH>', 'path to the cert file')
.option('--ca <PATH>', 'path to the ca certificate')
Expand Down Expand Up @@ -295,6 +298,7 @@ export class Commander {
.option('-u, --username <USER>', 'the username')
.option('-P, --password <PASS>', 'the password')
.option('-l, --protocol <PROTO>', 'the protocol to use, mqtt or mqtts', parseProtocol, 'mqtt')
.option('--path <PATH>', 'the path of websocket', '/mqtt')
.option('--key <PATH>', 'path to the key file')
.option('--cert <PATH>', 'path to the cert file')
.option('--ca <PATH>', 'path to the ca certificate')
Expand Down Expand Up @@ -391,6 +395,7 @@ export class Commander {
.option('-u, --username <USER>', 'the username')
.option('-P, --password <PASS>', 'the password')
.option('-l, --protocol <PROTO>', 'the protocol to use, mqtt or mqtts', parseProtocol, 'mqtt')
.option('--path <PATH>', 'the path of websocket', '/mqtt')
.option('--key <PATH>', 'path to the key file')
.option('--cert <PATH>', 'path to the cert file')
.option('--ca <PATH>', 'path to the ca certificate')
Expand Down Expand Up @@ -476,6 +481,7 @@ export class Commander {
.option('-u, --username <USER>', 'the username')
.option('-P, --password <PASS>', 'the password')
.option('-l, --protocol <PROTO>', 'the protocol to use, mqtt or mqtts', parseProtocol, 'mqtt')
.option('--path <PATH>', 'the path of websocket', '/mqtt')
.option('--key <PATH>', 'path to the key file')
.option('--cert <PATH>', 'path to the cert file')
.option('--ca <PATH>', 'path to the ca certificate')
Expand Down Expand Up @@ -574,6 +580,7 @@ export class Commander {
.option('-u, --username <USER>', 'the username')
.option('-P, --password <PASS>', 'the password')
.option('-l, --protocol <PROTO>', 'the protocol to use, mqtt or mqtts', parseProtocol, 'mqtt')
.option('--path <PATH>', 'the path of websocket', '/mqtt')
.option('--key <PATH>', 'path to the key file')
.option('--cert <PATH>', 'path to the cert file')
.option('--ca <PATH>', 'path to the ca certificate')
Expand Down
9 changes: 8 additions & 1 deletion cli/src/lib/pub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ const send = (
} else {
basicLog.published()
}
client.end()
// FIXME: When using the ws and wss protocols to connect, and QoS is 0, the message may not have been successfully sent when the publish callback is triggered. Therefore, delay closing the connection for 2 seconds.
if (['ws', 'wss'].includes(connOpts.protocol ?? '') && pubOpts.opts.qos === 0) {
setTimeout(() => {
client.end()
}, 2000)
} else {
client.end()
}
})
})
client.on('error', (err) => {
Expand Down
1 change: 1 addition & 0 deletions cli/src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ declare global {
username?: string
password?: string
protocol?: Protocol
path?: string
key?: string
cert?: string
ca?: string
Expand Down
6 changes: 4 additions & 2 deletions cli/src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const parseNumber = (value: string) => {
}

const parseProtocol = (value: string) => {
if (!['mqtt', 'mqtts'].includes(value)) {
signale.error('Only mqtt and mqtts are supported.')
if (!['mqtt', 'mqtts', 'ws', 'wss'].includes(value)) {
signale.error('Only mqtt, mqtts, ws and wss are supported.')
process.exit(1)
}
return value
Expand Down Expand Up @@ -156,6 +156,7 @@ const parseConnectOptions = (
username,
password,
protocol,
path,
key,
cert,
ca,
Expand Down Expand Up @@ -190,6 +191,7 @@ const parseConnectOptions = (
username,
password,
protocol,
path,
reconnectPeriod,
}

Expand Down

0 comments on commit d174f14

Please sign in to comment.