fix: mqtt client.end error in onUnmounted, update some style

This commit is contained in:
edgexie 2024-03-24 18:02:42 +08:00
parent 57e934e683
commit 5208eca0fa

View File

@ -25,19 +25,20 @@ const connection = reactive({
// topic/mqttx // topic/mqttx
const subscription = ref({ const subscription = ref({
topic: "topic/mqttx", topic: "topic/mqttx",
qos: 0 as mqtt.QoS qos: 0 as any
}); });
// topic/browser // topic/browser
const publish = ref({ const publish = ref({
topic: "topic/browser", topic: "topic/browser",
qos: 0 as mqtt.QoS, qos: 0 as any,
payload: '{ "msg": "Hello, I am browser." }' payload: '{ "msg": "Hello, I am browser." }'
}); });
let client = ref({ let client = ref({
connected: false connected: false
} as mqtt.MqttClient); } as mqtt.MqttClient);
const receivedMessages = ref(""); const receivedMessages = ref("");
const subscribedSuccess = ref(false); const subscribedSuccess = ref(false);
const btnLoadingType = ref(""); const btnLoadingType = ref("");
@ -175,14 +176,20 @@ const handleProtocolChange = (value: string) => {
}; };
onUnmounted(() => { onUnmounted(() => {
try {
if (client.value.end) {
client.value.end(); client.value.end();
console.log("disconnected successfully");
}
} catch (error) {
console.log(error);
}
}); });
</script> </script>
<template> <template>
<el-card shadow="never" :body-style="{ padding: '20px' }"> <el-card shadow="never" :body-style="{ padding: '20px' }">
<template #header> <template #header>
<p>
基于 基于
<el-link <el-link
type="primary" type="primary"
@ -200,12 +207,6 @@ onUnmounted(() => {
>EMQX</el-link >EMQX</el-link
> >
实现的一套 MQTT 客户端 实现的一套 MQTT 客户端
</p>
<p>
<el-text
>可将订阅主题设置为 topic/browser测试MQTT的自发自收</el-text
>
</p>
</template> </template>
<template #default> <template #default>
<el-card shadow="never"> <el-card shadow="never">
@ -287,7 +288,7 @@ onUnmounted(() => {
/> />
</el-form-item> </el-form-item>
</el-col> </el-col>
<el-col :span="4"> <el-col :span="8">
<el-form-item prop="qos" label="通信质量"> <el-form-item prop="qos" label="通信质量">
<el-select <el-select
v-model="subscription.qos" v-model="subscription.qos"
@ -302,7 +303,9 @@ onUnmounted(() => {
</el-select> </el-select>
</el-form-item> </el-form-item>
</el-col> </el-col>
<el-col :span="12"> </el-row>
<el-row>
<el-col>
<el-button <el-button
type="primary" type="primary"
class="sub-btn" class="sub-btn"
@ -331,7 +334,13 @@ onUnmounted(() => {
<el-form label-position="top" :model="publish"> <el-form label-position="top" :model="publish">
<el-row :gutter="20"> <el-row :gutter="20">
<el-col :span="8"> <el-col :span="8">
<el-form-item prop="topic" label="主题"> <el-form-item prop="topic">
<template #label>
<span>主题</span>
<el-text type="info" size="small">
可将订阅主题设置为topic/browser测试MQTT的自发自收
</el-text>
</template>
<el-input v-model="publish.topic" /> <el-input v-model="publish.topic" />
</el-form-item> </el-form-item>
</el-col> </el-col>
@ -354,7 +363,7 @@ onUnmounted(() => {
</el-col> </el-col>
</el-row> </el-row>
</el-form> </el-form>
<el-col :span="24" class="text-right"> <el-col :span="24">
<el-button <el-button
type="primary" type="primary"
:loading="btnLoadingType === 'publish'" :loading="btnLoadingType === 'publish'"