|
@@ -411,20 +411,20 @@ export default {
|
|
|
},
|
|
|
// mqtt相关方法
|
|
|
connectMQTTwo() {
|
|
|
- if (this.mqttClientTwo && this.mqttClientTwo.connected) {
|
|
|
- console.log("MQTT连接已存在,无需重新创建", this.mqttClientTwo);
|
|
|
- return;
|
|
|
- }
|
|
|
+ // if (this.mqttClientTwo && this.mqttClientTwo.connected) {
|
|
|
+ // console.log("MQTT连接已存在,无需重新创建", this.mqttClientTwo);
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
// 清理之前的连接
|
|
|
- if (this.mqttClientTwo) {
|
|
|
- try {
|
|
|
- this.mqttClientTwo.end();
|
|
|
- this.mqttClientTwo = null;
|
|
|
- console.log("清理旧MQTT连接");
|
|
|
- } catch (e) {
|
|
|
- console.error("清理连接时出错:", e);
|
|
|
- }
|
|
|
- }
|
|
|
+ // if (this.mqttClientTwo) {
|
|
|
+ // try {
|
|
|
+ // this.mqttClientTwo.end();
|
|
|
+ // this.mqttClientTwo = null;
|
|
|
+ // console.log("清理旧MQTT连接");
|
|
|
+ // } catch (e) {
|
|
|
+ // console.error("清理连接时出错:", e);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
|
|
|
const THRESHOLD = 2;
|
|
|
const params = {
|
|
@@ -486,7 +486,7 @@ export default {
|
|
|
client.on("error", (err) => {
|
|
|
setTimeout(() => {
|
|
|
this.connectMQTTwo();
|
|
|
- }, 5000);
|
|
|
+ }, 1000);
|
|
|
});
|
|
|
|
|
|
client.on("reconnect", () => {});
|
|
@@ -556,9 +556,20 @@ export default {
|
|
|
}
|
|
|
},
|
|
|
},
|
|
|
- created() {
|
|
|
- // 在组件挂载后连接MQTT
|
|
|
- this.connectMQTTwo();
|
|
|
+ mounted() {
|
|
|
+ setTimeout(() => {
|
|
|
+ this.connectMQTTwo();
|
|
|
+ console.log("组件挂载,连接MQTT");
|
|
|
+ }, 1000);
|
|
|
+ },
|
|
|
+ // beforeUpdate() {
|
|
|
+ // this.connectMQTTwo();
|
|
|
+ // console.log("组件更新2222,断开MQTT连接");
|
|
|
+ // },
|
|
|
+ beforeDestroy() {
|
|
|
+ // 在组件销毁前断开MQTT连接
|
|
|
+ this.closemqtt();
|
|
|
+ console.log("组件销毁3333,断开MQTT连接");
|
|
|
},
|
|
|
};
|
|
|
</script>
|