feat: mqtt调试
This commit is contained in:
@ -52,7 +52,7 @@
|
||||
import {
|
||||
Langlist
|
||||
} from "@/common/lang";
|
||||
import MQTTClient from "@/static/lib/mqtt.js";
|
||||
import mqtt from "@/uni_modules/leliven-mqtt/index.js";
|
||||
export default {
|
||||
|
||||
data() {
|
||||
@ -65,22 +65,10 @@
|
||||
mqttClient: null,
|
||||
backData: {},
|
||||
smallArr: [],
|
||||
mqttConfig: {
|
||||
host: '13.39.200.14',
|
||||
port: 8083,
|
||||
clientId: 'mqttx_' + Date.now(),
|
||||
username: 'admin',
|
||||
password: 'zzkj@688737',
|
||||
path: 'mqtt',
|
||||
protocolVersion: 4, // 4 for MQTT 3.1.1
|
||||
clean: false,
|
||||
cleanStart: false, // for MQTT 5.0
|
||||
keepAlive: 60,
|
||||
sessionExpiryInterval: 4294967295,
|
||||
connectTimeout: 10000,
|
||||
reconnectPeriod: 3000
|
||||
|
||||
},
|
||||
user: {
|
||||
username: 'admin', // 设置用户名
|
||||
password: 'zzkj@688737' // 设置密码
|
||||
}
|
||||
|
||||
};
|
||||
},
|
||||
@ -95,121 +83,47 @@
|
||||
},
|
||||
|
||||
onShow() {
|
||||
this.connect()
|
||||
this.initmqttClient()
|
||||
},
|
||||
|
||||
methods: {
|
||||
initMQTTClient() {
|
||||
this.mqttClient = new MQTTClient(this.mqttConfig)
|
||||
console.log(this.mqttClient);
|
||||
// 监听连接事件
|
||||
this.mqttClient.on('connect', () => {
|
||||
console.log('MQTT 连接成功')
|
||||
this.publish({
|
||||
topic: '/test',
|
||||
message: {
|
||||
fun: 'GET',
|
||||
type: 'WJ_Get_NewControlSystem',
|
||||
content: 0
|
||||
},
|
||||
qos: 0
|
||||
initmqttClient() {
|
||||
// 初始化客户端连接地址
|
||||
// #ifdef H5
|
||||
let mqttClient = mqtt.connect('ws://13.39.200.14:8083/mqtt', {
|
||||
...this.user
|
||||
})
|
||||
// #endif
|
||||
// #ifdef MP-WEIXIN||APP-PLUS
|
||||
let mqttClient = mqtt.connect('wx://13.39.200.14:8083/mqtt', {
|
||||
...this.user
|
||||
})
|
||||
// #endif
|
||||
console.log(mqttClient,'888')
|
||||
this.mqttClient = mqttClient
|
||||
// 连接MQ服务
|
||||
mqttClient.on('connect', function(res) {
|
||||
console.log('连接成功')
|
||||
console.log(res)
|
||||
|
||||
|
||||
// 订阅主题
|
||||
mqttClient.subscribe('presence', function(err) {
|
||||
if (!err) {
|
||||
// 订阅成功
|
||||
mqttClient.publish('presence', 'hello mqtt')
|
||||
}
|
||||
})
|
||||
}).on('reconnect', function() {
|
||||
console.log('重新连接')
|
||||
}).on('error', function() {
|
||||
console.log('连接失败')
|
||||
}).on('end', function() {
|
||||
console.log('连接End')
|
||||
}).on('message', function(topic, message) {
|
||||
// 收到MQ消息
|
||||
console.log(message.toString())
|
||||
})
|
||||
|
||||
// 监听断开事件
|
||||
this.mqttClient.on('disconnect', (res) => {
|
||||
this.connectionStatus = 'disconnected'
|
||||
console.log('连接已关闭')
|
||||
})
|
||||
|
||||
// 监听错误事件
|
||||
this.mqttClient.on('error', (error) => {
|
||||
console.log(error)
|
||||
})
|
||||
|
||||
// 监听状态变化
|
||||
this.mqttClient.on('statusChange', (status) => {
|
||||
console.log(status)
|
||||
})
|
||||
|
||||
// 监听消息事件
|
||||
this.mqttClient.on('message', (msg) => {
|
||||
console.log(msg)
|
||||
})
|
||||
|
||||
// 监听订阅成功事件
|
||||
this.mqttClient.on('subscribed', (packetId) => {
|
||||
this.addLog('success', `订阅成功 (包ID: ${packetId})`)
|
||||
uni.showToast({
|
||||
title: '订阅成功',
|
||||
icon: 'success'
|
||||
})
|
||||
})
|
||||
|
||||
// 监听取消订阅成功事件
|
||||
this.mqttClient.on('unsubscribed', (packetId) => {
|
||||
this.addLog('success', `取消订阅成功 (包ID: ${packetId})`)
|
||||
uni.showToast({
|
||||
title: '取消订阅成功',
|
||||
icon: 'success'
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
|
||||
publish(publishConfig) {
|
||||
const {
|
||||
topic,
|
||||
message,
|
||||
qos
|
||||
} = publishConfig
|
||||
|
||||
if (!topic) {
|
||||
uni.showToast({
|
||||
title: '请输入主题',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if (this.mqttClient) {
|
||||
this.mqttClient.publish(topic, message, qos)
|
||||
}
|
||||
},
|
||||
|
||||
subscribe() {
|
||||
|
||||
if (this.mqttClient) {
|
||||
this.mqttClient.subscribe({
|
||||
topic: '/test',
|
||||
qos: 0
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
connect() {
|
||||
const {
|
||||
host
|
||||
} = this.mqttConfig
|
||||
|
||||
if (!host) {
|
||||
uni.showToast({
|
||||
title: '请输入主机地址',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
console.log('开始连接 MQTT 服务器...')
|
||||
|
||||
|
||||
// 如果客户端未初始化,重新初始化
|
||||
if (!this.mqttClient) {
|
||||
this.initMQTTClient()
|
||||
}
|
||||
this.mqttClient.reSetconstructor(this.mqttConfig)
|
||||
|
||||
this.mqttClient.connect()
|
||||
},
|
||||
|
||||
|
||||
@ -301,21 +215,19 @@
|
||||
pre[cur.prop] = Number(pre[cur.prop])
|
||||
return pre
|
||||
}, {})
|
||||
this.publish({
|
||||
topic: '/test',
|
||||
message: {
|
||||
fun: 'SET',
|
||||
type: 'WJ_Set_NewControlSystem',
|
||||
content: {
|
||||
...this.backData,
|
||||
params: {
|
||||
...this.backData.params,
|
||||
...smallParam
|
||||
}
|
||||
console.log(this.mqttClient)
|
||||
this.mqttClient.publish('presence', JSON.stringify({
|
||||
fun: 'SET',
|
||||
type: 'WJ_Set_NewControlSystem',
|
||||
content: {
|
||||
...this.backData,
|
||||
params: {
|
||||
...this.backData.params,
|
||||
...smallParam
|
||||
}
|
||||
},
|
||||
qos: 0
|
||||
})
|
||||
}
|
||||
}))
|
||||
|
||||
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user