Skip to content

Commit

Permalink
feat(cli): display the reason and code for the disconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
XL-YiBai committed Mar 30, 2024
1 parent c5bb6ee commit 730fb73
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 17 deletions.
10 changes: 5 additions & 5 deletions cli/src/lib/conn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ const conn = (options: ConnectOptions) => {
basicLog.close()
})

client.on('disconnect', () => {
basicLog.disconnect()
client.on('disconnect', (packet: IDisconnectPacket) => {
basicLog.disconnect(packet)
})
}

Expand Down Expand Up @@ -78,7 +78,7 @@ const benchConn = async (options: BenchConnectOptions) => {
const start = Date.now()

for (let i = 1; i <= count; i++) {
;((i: number, connOpts: mqtt.IClientOptions) => {
; ((i: number, connOpts: mqtt.IClientOptions) => {
const opts = { ...connOpts }

opts.clientId = clientId.includes('%i') ? clientId.replaceAll('%i', i.toString()) : `${clientId}_${i}`
Expand Down Expand Up @@ -124,8 +124,8 @@ const benchConn = async (options: BenchConnectOptions) => {
benchLog.close(connectedCount, count, opts.clientId!)
})

client.on('disconnect', () => {
basicLog.disconnect(opts.clientId!)
client.on('disconnect', (packet: IDisconnectPacket) => {
basicLog.disconnect(packet, opts.clientId!)
})
})(i, connOpts)

Expand Down
8 changes: 4 additions & 4 deletions cli/src/lib/pub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ const multisend = (
reconnectPeriod ? sender.cork() : process.exit(1)
})

client.on('disconnect', () => {
basicLog.disconnect()
client.on('disconnect', (packet: IDisconnectPacket) => {
basicLog.disconnect(packet)
})
}

Expand Down Expand Up @@ -354,8 +354,8 @@ const multiPub = async (commandType: CommandType, options: BenchPublishOptions |
benchLog.close(connectedCount, count, opts.clientId!)
})

client.on('disconnect', () => {
basicLog.disconnect(opts.clientId!)
client.on('disconnect', (packet: IDisconnectPacket) => {
basicLog.disconnect(packet, opts.clientId!)
})
})(i, connOpts)

Expand Down
8 changes: 4 additions & 4 deletions cli/src/lib/sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ const sub = (options: SubscribeOptions) => {
!outputModeClean && basicLog.close()
})

client.on('disconnect', () => {
!outputModeClean && basicLog.disconnect()
client.on('disconnect', (packet: IDisconnectPacket) => {
!outputModeClean && basicLog.disconnect(packet)
})
}

Expand Down Expand Up @@ -286,8 +286,8 @@ const benchSub = async (options: BenchSubscribeOptions) => {
benchLog.close(connectedCount, count, opts.clientId!)
})

client.on('disconnect', () => {
basicLog.disconnect(opts.clientId!)
client.on('disconnect', (packet: IDisconnectPacket) => {
basicLog.disconnect(packet, opts.clientId!)
})
})(i, connOpts)

Expand Down
9 changes: 9 additions & 0 deletions cli/src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ declare global {
interface LsOptions {
scenarios: boolean
}

interface IDisconnectPacket {
cmd: 'disconnect',
qos: QoS
dup: boolean
retain: boolean
reasonCode: number,
length: number
}
}

export {}
48 changes: 48 additions & 0 deletions cli/src/utils/mqttErrorReason.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const mqttErrorReason: Record<number, string> = {
4: 'Disconnect with Will Message',
16: 'No matching subscribers',
17: 'No subscription existed',
24: 'Continue authentication',
25: 'Re-authenticate',
128: 'Unspecified error',
129: 'Malformed Packet',
130: 'Protocol Error',
131: 'Implementation specific error',
132: 'Unsupported Protocol Version',
133: 'Client Identifier not valid',
134: 'Bad User Name or Password',
135: 'Not authorized',
136: 'Server unavailable',
137: 'Server busy',
138: 'Banned',
139: 'Server shutting down',
140: 'Bad authentication method',
141: 'Keep Alive timeout',
142: 'Session taken over',
143: 'Topic Filter invalid',
144: 'Topic Name invalid',
145: 'Packet Identifier in use',
146: 'Packet Identifier not found',
147: 'Receive Maximum exceeded',
148: 'Topic Alias invalid',
149: 'Packet too large',
150: 'Message rate too high',
151: 'Quota exceeded',
152: 'Administrative action',
153: 'Payload format invalid',
154: 'Retain not supported',
155: 'QoS not supported',
156: 'Use another server',
157: 'Server moved',
158: 'Shared Subscriptions not supported',
159: 'Connection rate exceeded',
160: 'Maximum connect time',
161: 'Subscription Identifiers not supported',
162: 'Wildcard Subscriptions not supported',
}

const getErrorReason = (code: number) => {
return mqttErrorReason[code] ?? 'Unknown error'
}

export default getErrorReason
11 changes: 7 additions & 4 deletions cli/src/utils/signale.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Signale } from 'signale'
import chalk from 'chalk'
import { inspect } from 'util'
import getErrorReason from './mqttErrorReason'

const option = {
config: {
Expand Down Expand Up @@ -30,8 +31,7 @@ const basicLog = {
signale.await('Connecting...')
} else {
signale.await(
`Connecting using configuration file, host: ${host}, port: ${port}${topic ? `, topic: ${topic}` : ''}${
message ? `, message: ${message}` : ''
`Connecting using configuration file, host: ${host}, port: ${port}${topic ? `, topic: ${topic}` : ''}${message ? `, message: ${message}` : ''
}`,
)
}
Expand All @@ -48,8 +48,11 @@ const basicLog = {
close: () => signale.error('Connection closed'),
reconnecting: () => signale.await('Reconnecting...'),
reconnectTimesLimit: () => signale.error('Exceed the maximum reconnect times limit, stop retry'),
disconnect: (clientId?: string) =>
signale.warn(`${clientId ? `Client ID: ${clientId}, ` : ''}The Broker has actively disconnected`),
disconnect: (packet: IDisconnectPacket, clientId?: string) => {
const { reasonCode } = packet
const reason = reasonCode === 0 ? 'Normal disconnection' : getErrorReason(reasonCode)
signale.warn(`${clientId ? `Client ID: ${clientId}, ` : ''}The Broker has actively disconnected, Reason: ${reason} (Code: ${reasonCode})`)
}
}

const benchLog = {
Expand Down

0 comments on commit 730fb73

Please sign in to comment.