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(desktop): connection support disconnect and offline event #1549

Merged
merged 2 commits into from Jan 9, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/lang/connections.ts
Expand Up @@ -866,4 +866,11 @@ export default {
ja: '保持メッセージの取り扱い',
hu: 'Üzenetmegőrzés kezelése',
},
onDisconnect: {
zh: '服务器已主动断开连接',
en: 'The Broker has actively disconnected',
tr: 'Sunucu aktif bir şekilde bağlantıyı kesmiştir',
ja: 'サーバーから積極的に接続が切断されました',
hu: 'A kiszolgáló aktívan bontotta a kapcsolatot',
},
}
42 changes: 34 additions & 8 deletions src/views/connections/ConnectionsDetail.vue
Expand Up @@ -281,7 +281,7 @@
import { Component, Vue, Prop, Watch } from 'vue-property-decorator'
import { Getter, Action } from 'vuex-class'
import { ipcRenderer } from 'electron'
import { MqttClient, IConnackPacket, IPublishPacket, IClientPublishOptions } from 'mqtt'
import { MqttClient, IConnackPacket, IPublishPacket, IClientPublishOptions, IDisconnectPacket } from 'mqtt'
import _ from 'lodash'
import { Subject, fromEvent } from 'rxjs'
import { bufferTime, map, filter, takeUntil } from 'rxjs/operators'
Expand Down Expand Up @@ -567,6 +567,8 @@ export default class ConnectionsDetail extends Vue {
this.client.on('connect', this.onConnect)
this.client.on('error', this.onError)
this.client.on('reconnect', this.onReConnect)
this.client.on('disconnect', this.onDisconnect)
this.client.on('offline', this.onOffline)
this.onMessageArrived(this.client as MqttClient, id)
}

Expand All @@ -586,7 +588,7 @@ export default class ConnectionsDetail extends Vue {
} catch (error) {
const err = error as Error
this.connectLoading = false
this.notifyErrorWithCopilot(err.toString())
this.notifyMsgWithCopilot(err.toString())
}
}

Expand Down Expand Up @@ -933,7 +935,7 @@ export default class ConnectionsDetail extends Vue {
}

private handleSubTopicError(errMsg: string, info?: string) {
this.notifyErrorWithCopilot(errMsg, info, () => {
this.notifyMsgWithCopilot(errMsg, info, () => {
this.subListRef.showDialog = false
})
}
Expand Down Expand Up @@ -1021,7 +1023,7 @@ export default class ConnectionsDetail extends Vue {
msgTitle = error.toString()
}
this.forceCloseTheConnection()
this.notifyErrorWithCopilot(msgTitle)
this.notifyMsgWithCopilot(msgTitle)
this.$log.error(`${this.record.name} connect fail, MQTT.js onError trigger, ${error.stack}`)
this.$emit('reload')
}
Expand Down Expand Up @@ -1065,6 +1067,22 @@ export default class ConnectionsDetail extends Vue {
this.connectLoading = false
}

// Emitted after receiving disconnect packet from broker. MQTT 5.0 feature.
private onDisconnect(packet: IDisconnectPacket) {
this.notifyMsgWithCopilot(this.$tc('connections.onDisconnect'), JSON.stringify(packet), () => {}, 'warning')
const logMessage = `Received disconnect packet from Broker. MQTT.js onDisconnect trigger, the packet details: ${JSON.stringify(
packet,
)}`
this.$log.warn(logMessage)
}

// Emitted when the client goes offline.
private onOffline() {
this.$log.info(
`The connection ${this.record.name} (clientID ${this.record.clientId}) is offline. MQTT.js onOffline trigger.`,
)
}

private forceCloseTheConnection() {
this.client.end!(true)
this.reTryConnectTimes = 0
Expand Down Expand Up @@ -1785,11 +1803,19 @@ export default class ConnectionsDetail extends Vue {
}

/**
* Notifies the user with an error message using the Copilot feature.
* Notifies the user with a message and provides an option to ask Copilot for assistance.
*
* @param {string} msgTitle - The title of the error message.
* @param {string} msgTitle - The title of the message.
* @param {string} promptInfo - Additional information to prompt the user.
* @param {function} callback - The callback function to be executed after asking Copilot.
* @param {string} type - The type of the message ('error' or 'warning') defualt error.
*/
private notifyErrorWithCopilot(msgTitle: string, promptInfo?: string, callback?: () => void) {
private notifyMsgWithCopilot(
msgTitle: string,
promptInfo?: string,
callback?: () => void,
type: 'error' | 'warning' = 'error',
) {
const askCopilotButton = `
<button id="notify-copilot-button">Ask Copilot</button>
`
Expand All @@ -1798,7 +1824,7 @@ export default class ConnectionsDetail extends Vue {
title: msgTitle,
dangerouslyUseHTMLString: true,
message,
type: 'error',
type,
duration: 4000,
offset: 30,
})
Expand Down