From 9cc59c17892a7e69de54e06b5931e78c9b05551c Mon Sep 17 00:00:00 2001
From: zhuguifei <zhuguifei@zhuguifeideiMac.local>
Date: 星期二, 02 九月 2025 09:24:34 +0800
Subject: [PATCH] 添加mqtt登出
---
mqtt/MqttView.vue | 199 +++++++++++++++++++++++++++++++------------------
1 files changed, 124 insertions(+), 75 deletions(-)
diff --git a/mqtt/MqttView.vue b/mqtt/MqttView.vue
index 412d8bf..33c97d2 100644
--- a/mqtt/MqttView.vue
+++ b/mqtt/MqttView.vue
@@ -4,7 +4,6 @@
</template>
<script>
-
export default {
name: 'MqttView',
data() {
@@ -13,92 +12,142 @@
}
},
onReady() {
-
+
+
+ },
+ destroyed() {
+ console.error("MQTT缁勪欢閿�姣�")
+ this.$mqttTool.end().then(res => {
+ console.error(res)
+ })
},
methods: {
- initMqtt(){
+ initMqtt() {
uni.getSystemInfo({
success: (res) => {
let deviceId = res.deviceId
if (!deviceId) {
deviceId = 'mobile-' + this.tenantId + '-' + Date.parse(new Date())
}
- uni.setStorageSync(this.$constant.DEVICE_ID, 'mobile-' + this.tenantId + '-' + res.deviceId);
+ uni.setStorageSync(this.$constant.DEVICE_ID, 'mobile-' + this.tenantId + '-' + res
+ .deviceId);
}
})
- this.startConnect();
+ this.startConnect();
},
- /* 杩炴帴MQTT */
- async startConnect() {
- var _this = this
- const account = uni.getStorageSync('account');
- const deviceid = uni.getStorageSync(this.$constant.DEVICE_ID);
-
- if (!account) {
-
- return false
- }
- let opts = {
- // #ifdef H5
- url: 'ws://' + this.$api.mqttBaseUrl + ':8083/mqtt',
- // #endif
- // #ifdef APP-PLUS
- url: 'wx://' + this.$api.mqttBaseUrl + ':8083/mqtt',
- // #endif
- clientId: deviceid,
- username: account.username,
- password: account.password
- }
- if (!this.$mqttTool.client) {
- var client = await this.$mqttTool.connect(opts);
- }
- //璁㈤槄鏌ヨ璁惧鐘舵�佽繑鍥炴暟鎹�
- this.$mqttTool.subscribe({
- topic: this.$constant.SERVICE_DOWN + '/' + deviceid + '/#',
- qos: 0
- }).then(res => {
- console.error(res)
- })
-
- //璁㈤槄璁惧鏁呴殰骞挎挱锛堝箍鎾笉鍦ㄤ箮瀹㈡埛绔痠d锛屽彂閫佺粰绉熸埛涓嬫墍鏈夊湪绾跨殑璁惧锛�
- this.$mqttTool.subscribe({
- topic: this.$constant.SERVICE_BROADCAST_TENANT_REAL_FAULT.replace('%s', this.tenantId),
- qos: 0
- }).then(res => {
- console.error(res)
- })
- //璁㈤槄鍙戦�佹寚浠よ繑鍥炵粨鏋�
- // this.$mqttTool.subscribe({
- // topic: this.$constant.SERVICE_RES_EQU_CMD,
- // qos: 0
- // }).then(res => {
- // console.error(res)
- // })
- // if (!client) {
- // return false
- // }
+ /* 杩炴帴MQTT */
+ async startConnect() {
+ var _this = this
+ const account = uni.getStorageSync('account');
+ const deviceid = uni.getStorageSync(this.$constant.DEVICE_ID);
+
+ if (!account) {
+
+ return false
+ }
+ let opts = {
+ // #ifdef H5
+ url: 'wss://' + this.$api.mqttBaseUrl + ':8084/mqtt',
+ // #endif
+ // #ifdef MP-WEIXIN
+ url: 'wxs://' + this.$api.mqttBaseUrl + ':8084/mqtt', // 寰俊灏忕▼搴忓己鍒� WSS
+ // #endif
+ // #ifdef APP-PLUS
+ url: 'wxs://' + this.$api.mqttBaseUrl + ':8084/mqtt', // Android/iOS 鐢ㄦ櫘閫� WebSocket
+ // #endif
+ clientId: deviceid,
+ username: account.username,
+ password: account.password
+ }
+ if (!this.$mqttTool.client) {
+ this.$mqttTool.client = await this.$mqttTool.connect(opts);
+ }
+ //璁㈤槄鏌ヨ璁惧鐘舵�佽繑鍥炴暟鎹�
+ this.$mqttTool.subscribe({
+ topic: this.$constant.SERVICE_DOWN + '/' + deviceid + '/#',
+ qos: 0
+ }).then(res => {
+ console.error(res)
+ })
+
+ //璁㈤槄璁惧鏁呴殰骞挎挱锛堝箍鎾笉鍦ㄤ箮瀹㈡埛绔痠d锛屽彂閫佺粰绉熸埛涓嬫墍鏈夊湪绾跨殑璁惧锛�
+ this.$mqttTool.subscribe({
+ topic: this.$constant.SERVICE_BROADCAST_TENANT_REAL_FAULT.replace('%s', this.tenantId),
+ qos: 0
+ }).then(res => {
+ console.error(res)
+ })
+
+ //璁㈤槄璁惧瀹炴椂鏁呴殰锛堜富鍔ㄨ姹傚彧鍙戠粰璇锋眰璁惧锛�
+ this.$mqttTool.subscribe({
+ topic: this.$constant.SERVICE_ONECE_TENANT_REAL_FAULT.replace('%s', deviceid),
+ qos: 0
+ }).then(res => {
+ console.error(res)
+ })
+
+ //璁㈤槄骞茬嚗璁惧杩炴帴鏂紑鐘舵�佹洿鏂�-绉熸埛鍐呮墍鏈夎澶�
+ this.$mqttTool.subscribe({
+ topic: this.$constant.SERVICE_BROADCAST_TENANT_UPDATE_EQU_STATU.replace('%s', this
+ .tenantId),
+ qos: 0
+ }).then(res => {
+ console.error(res)
+ })
+ //璁㈤槄鏌ヨ骞茬嚗璁惧杩炴帴鐘舵��-鍗曚釜璁惧
+ this.$mqttTool.subscribe({
+ topic: this.$constant.SERVICE_RES_EQU_STATU.replace('%s', deviceid),
+ qos: 0
+ }).then(res => {
+ console.error(res)
+ })
+
+ //璁㈤槄骞茬嚗璁惧瀹炴椂鏁版嵁
+ this.$mqttTool.subscribe({
+ topic: this.$constant.SERVICE_BROADCAST_TENANT_REAL_DATA.replace('%s', this.tenantId),
+ qos: 0
+ }).then(res => {
+ console.error(res)
+ })
+
+ //璁㈤槄鍙戦�佹寚浠よ繑鍥炵粨鏋�
+ // this.$mqttTool.subscribe({
+ // topic: this.$constant.SERVICE_RES_EQU_CMD,
+ // qos: 0
+ // }).then(res => {
+ // console.error(res)
+ // })
+ // if (!client) {
+ // return false
+ // }
let that = this
-
- client.on('connect', function(res) {
- console.error('杩炴帴鎴愬姛')
- })
- client.on('reconnect', function(res) {
- console.error('閲嶆柊杩炴帴')
- })
- client.on('error', function(res) {
- console.info('杩炴帴閿欒')
- })
- client.on('close', function(res) {
-
- console.error('鍏抽棴鎴愬姛')
-
- })
- client.on('message', function(topic, message, buffer) {
- that.$eventBus.$emit(that.$constant.MQTT_TOPIC_MESSAGE, message);
- console.info(message)
-
- })
- },
+ let client = this.$mqttTool.client
+
+ client.on('connect', function(res) {
+ console.error('杩炴帴鎴愬姛')
+ console.error(res)
+ })
+ client.on('reconnect', function(res) {
+ console.error('閲嶆柊杩炴帴')
+ console.error(res)
+ })
+ client.on('error', function(res) {
+ console.info('杩炴帴閿欒')
+ console.error(res)
+ })
+ client.on('close', function(res) {
+
+ console.error('鍏抽棴鎴愬姛')
+ console.error(res)
+
+ })
+ client.on('message', function(topic, message, buffer) {
+ uni.$emit(that.$constant.MQTT_TOPIC_MESSAGE, message);
+ //console.error("鏀跺埌message锛堟�伙級锛�"+topic)
+
+
+ })
+ },
},
computed: {
tenantId() {
--
Gitblit v1.9.3