Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): support auto reconnect #1134

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export class Commander {
.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(
'-rp, --reconnect-period <MILLISECONDS>',
'Interval between two reconnections, disable auto reconnect by setting to 0 (default: 1000ms)',
parseNumber,
1000,
)
// properties options of MQTT 5.0
.option('-se, --session-expiry-interval <SECONDS>', 'the session expiry interval in seconds', parseNumber)
.option('--rcv-max, --receive-maximum <NUMBER>', 'the receive maximum value', parseNumber)
Expand Down Expand Up @@ -128,6 +134,12 @@ export class Commander {
.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(
'-rp, --reconnect-period <MILLISECONDS>',
'Interval between two reconnections, disable auto reconnect by setting to 0 (default: 1000ms)',
parseNumber,
1000,
)
// connect properties options of MQTT 5.0
.option('-se, --session-expiry-interval <SECONDS>', 'the session expiry interval in seconds', parseNumber)
.option('--rcv-max, --receive-maximum <NUMBER>', 'the receive maximum value', parseNumber)
Expand Down Expand Up @@ -193,6 +205,12 @@ export class Commander {
.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(
'-rp, --reconnect-period <MILLISECONDS>',
'Interval between two reconnections, disable auto reconnect by setting to 0 (default: 1000ms)',
parseNumber,
1000,
)
// connect properties options of MQTT 5.0
.option('-se, --session-expiry-interval <SECONDS>', 'the session expiry interval in seconds', parseNumber)
.option('--rcv-max, --receive-maximum <NUMBER>', 'the receive maximum value', parseNumber)
Expand Down
8 changes: 8 additions & 0 deletions cli/src/lib/conn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ const conn = (options: ConnectOptions) => {
signale.error(err)
client.end()
})

client.on('reconnect', () => {
signale.await('Reconnecting...')
})

client.on('close', () => {
signale.error('Connection closed')
})
}

const benchConn = async (options: BenchConnectOptions) => {
Expand Down
31 changes: 25 additions & 6 deletions cli/src/lib/pub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const multisend = (
connOpts: IClientOptions,
pubOpts: { topic: string; message: string | Buffer; opts: IClientPublishOptions },
) => {
let isNewConnection = true
const client = mqtt.connect(connOpts)
signale.await('Connecting...')
const sender = new Writable({
Expand All @@ -49,12 +50,30 @@ const multisend = (

client.on('connect', () => {
signale.success('Connected, press Enter to publish, press Ctrl+C to exit')
pump(process.stdin, split2(), sender, (err) => {
client.end()
if (err) {
throw err
}
})
isNewConnection &&
pump(process.stdin, split2(), sender, (err) => {
client.end()
if (err) {
throw err
}
})
})

client.on('error', (err) => {
signale.error(err)
client.end()
})

client.on('reconnect', () => {
signale.await('Reconnecting...')
isNewConnection = false
sender.uncork()
})

client.on('close', () => {
signale.error('Connection closed')
const { reconnectPeriod } = connOpts
reconnectPeriod ? sender.cork() : process.exit(1)
})
}

Expand Down
8 changes: 8 additions & 0 deletions cli/src/lib/sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ const sub = (options: SubscribeOptions) => {
signale.error(err)
client.end()
})

client.on('reconnect', () => {
signale.await('Reconnecting...')
})

client.on('close', () => {
signale.error('Connection closed')
})
}

const benchSub = async (options: BenchSubscribeOptions) => {
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 @@ -21,6 +21,7 @@ declare global {
cert?: string
ca?: string
insecure?: boolean
reconnectPeriod: number
// properties of MQTT 5.0
sessionExpiryInterval?: number
receiveMaximum?: number
Expand Down
2 changes: 2 additions & 0 deletions cli/src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const parseConnectOptions = (
cert,
ca,
insecure,
reconnectPeriod,
sessionExpiryInterval,
receiveMaximum,
maximumPacketSize,
Expand Down Expand Up @@ -119,6 +120,7 @@ const parseConnectOptions = (
username,
password,
protocol,
reconnectPeriod,
}

if (key) {
Expand Down