diff --git a/cli/src/index.ts b/cli/src/index.ts index 66f73f6ce..10daafbd5 100755 --- a/cli/src/index.ts +++ b/cli/src/index.ts @@ -52,6 +52,7 @@ export class Commander { .option('-u, --username ', 'the username') .option('-P, --password ', 'the password') .option('-l, --protocol ', 'the protocol to use, mqtt or mqtts', parseProtocol, 'mqtt') + .option('--path ', 'the path of websocket', '/mqtt') .option('--key ', 'path to the key file') .option('--cert ', 'path to the cert file') .option('--ca ', 'path to the ca certificate') @@ -142,6 +143,7 @@ export class Commander { .option('-u, --username ', 'the username') .option('-P, --password ', 'the password') .option('-l, --protocol ', 'the protocol to use, mqtt or mqtts', parseProtocol, 'mqtt') + .option('--path ', 'the path of websocket', '/mqtt') .option('--key ', 'path to the key file') .option('--cert ', 'path to the cert file') .option('--ca ', 'path to the ca certificate') @@ -229,6 +231,7 @@ export class Commander { .option('-u, --username ', 'the username') .option('-P, --password ', 'the password') .option('-l, --protocol ', 'the protocol to use, mqtt or mqtts', parseProtocol, 'mqtt') + .option('--path ', 'the path of websocket', '/mqtt') .option('--key ', 'path to the key file') .option('--cert ', 'path to the cert file') .option('--ca ', 'path to the ca certificate') @@ -295,6 +298,7 @@ export class Commander { .option('-u, --username ', 'the username') .option('-P, --password ', 'the password') .option('-l, --protocol ', 'the protocol to use, mqtt or mqtts', parseProtocol, 'mqtt') + .option('--path ', 'the path of websocket', '/mqtt') .option('--key ', 'path to the key file') .option('--cert ', 'path to the cert file') .option('--ca ', 'path to the ca certificate') @@ -391,6 +395,7 @@ export class Commander { .option('-u, --username ', 'the username') .option('-P, --password ', 'the password') .option('-l, --protocol ', 'the protocol to use, mqtt or mqtts', parseProtocol, 'mqtt') + .option('--path ', 'the path of websocket', '/mqtt') .option('--key ', 'path to the key file') .option('--cert ', 'path to the cert file') .option('--ca ', 'path to the ca certificate') @@ -476,6 +481,7 @@ export class Commander { .option('-u, --username ', 'the username') .option('-P, --password ', 'the password') .option('-l, --protocol ', 'the protocol to use, mqtt or mqtts', parseProtocol, 'mqtt') + .option('--path ', 'the path of websocket', '/mqtt') .option('--key ', 'path to the key file') .option('--cert ', 'path to the cert file') .option('--ca ', 'path to the ca certificate') @@ -574,6 +580,7 @@ export class Commander { .option('-u, --username ', 'the username') .option('-P, --password ', 'the password') .option('-l, --protocol ', 'the protocol to use, mqtt or mqtts', parseProtocol, 'mqtt') + .option('--path ', 'the path of websocket', '/mqtt') .option('--key ', 'path to the key file') .option('--cert ', 'path to the cert file') .option('--ca ', 'path to the ca certificate') diff --git a/cli/src/lib/pub.ts b/cli/src/lib/pub.ts index 32a689314..9cf3249fb 100644 --- a/cli/src/lib/pub.ts +++ b/cli/src/lib/pub.ts @@ -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) => { diff --git a/cli/src/types/global.d.ts b/cli/src/types/global.d.ts index 0f1ebe228..9b7d9e99e 100644 --- a/cli/src/types/global.d.ts +++ b/cli/src/types/global.d.ts @@ -23,6 +23,7 @@ declare global { username?: string password?: string protocol?: Protocol + path?: string key?: string cert?: string ca?: string diff --git a/cli/src/utils/parse.ts b/cli/src/utils/parse.ts index 59d37bc2e..5b3b5efd9 100644 --- a/cli/src/utils/parse.ts +++ b/cli/src/utils/parse.ts @@ -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 @@ -156,6 +156,7 @@ const parseConnectOptions = ( username, password, protocol, + path, key, cert, ca, @@ -190,6 +191,7 @@ const parseConnectOptions = ( username, password, protocol, + path, reconnectPeriod, }