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 authored and ysfscream committed Apr 1, 2024
1 parent 9831891 commit f0a2602
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 27 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
10 changes: 5 additions & 5 deletions src/lang/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -846,11 +846,11 @@ export default {
hu: 'Üzenetmegőrzés kezelése',
},
onDisconnect: {
zh: '服务器已主动断开连接, Reason: {0} (Code: {1})',
en: 'The Broker has actively disconnected, Reason: {0} (Code: {1})',
tr: 'Sunucu aktif bir şekilde bağlantıyı kesmiştir, Reason: {0} (Code: {1})',
ja: 'サーバーから積極的に接続が切断されました, Reason: {0} (Code: {1})',
hu: 'A kiszolgáló aktívan bontotta a kapcsolatot, Reason: {0} (Code: {1})',
zh: '服务器已主动断开连接, Reason: {reason} (Code: {reasonCode})',
en: 'The Broker has actively disconnected, Reason: {reason} (Code: {reasonCode})',
tr: 'Sunucu aktif bir şekilde bağlantıyı kesmiştir, Reason: {reason} (Code: {reasonCode})',
ja: 'サーバーから積極的に接続が切断されました, Reason: {reason} (Code: {reasonCode})',
hu: 'A kiszolgáló aktívan bontotta a kapcsolatot, Reason: {reason} (Code: {reasonCode})',
},
hideConnections: {
zh: '隐藏连接列表',
Expand Down
2 changes: 1 addition & 1 deletion src/views/connections/ConnectionsDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ export default class ConnectionsDetail extends Vue {
const reasonCode = packet.reasonCode!
const reason = reasonCode === 0 ? 'Normal disconnection' : getErrorReason('5.0', reasonCode)
this.notifyMsgWithCopilot(
this.$t('connections.onDisconnect', [reason, reasonCode]) as string,
this.$t('connections.onDisconnect', { reason, reasonCode }) as string,
JSON.stringify(packet),
() => {},
'warning',
Expand Down
6 changes: 3 additions & 3 deletions web/src/lang/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ export default {
ja: '保持メッセージの取り扱い',
},
onDisconnect: {
zh: '服务器已主动断开连接, Reason: {0} (Code: {1})',
en: 'The Broker has actively disconnected, Reason: {0} (Code: {1})',
ja: 'サーバーから積極的に接続が切断されました, Reason: {0} (Code: {1})',
zh: '服务器已主动断开连接, Reason: {reason} (Code: {reasonCode})',
en: 'The Broker has actively disconnected, Reason: {reason} (Code: {reasonCode})',
ja: 'サーバーから積極的に接続が切断されました, Reason: {reason} (Code: {reasonCode})',
},
}
2 changes: 1 addition & 1 deletion web/src/views/connections/ConnectionsDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ export default class ConnectionsDetail extends Vue {
const reasonCode = packet.reasonCode!
const reason = reasonCode === 0 ? 'Normal disconnection' : getErrorReason('5.0', reasonCode)
this.$notify({
title: this.$t('connections.onDisconnect', [reason, reasonCode]) as string,
title: this.$t('connections.onDisconnect', { reason, reasonCode }) as string,
message: '',
type: 'warning',
duration: 3000,
Expand Down

0 comments on commit f0a2602

Please sign in to comment.