协议修改

This commit is contained in:
pengqiao1993
2026-03-10 16:11:41 +08:00
parent 4e73084a56
commit 085e76d179

View File

@ -41,7 +41,6 @@
disabled disabled
@click="selectShow(item, index)" @click="selectShow(item, index)"
/> />
<div></div>
</view> </view>
<view class=""> <view class="">
{{ item.selectLabel || $t("homePage.alarm.placeSelect") }} {{ item.selectLabel || $t("homePage.alarm.placeSelect") }}
@ -78,7 +77,7 @@
import { formList } from "@/common/form.js"; import { formList } from "@/common/form.js";
import { Langlist } from "@/common/lang"; import { Langlist } from "@/common/lang";
import pako from "pako"; import pako from "pako";
import mqtt from "mqtt"; import * as Paho from "paho-mqtt";
export default { export default {
data() { data() {
return { return {
@ -115,7 +114,7 @@ export default {
}, },
beforeDestroy() { beforeDestroy() {
if (this.mqttClient) { if (this.mqttClient) {
this.mqttClient.end() this.mqttClient.disconnect()
this.mqttClient = null this.mqttClient = null
} }
}, },
@ -194,72 +193,66 @@ export default {
} }
}, },
initMQTT() { initMQTT() {
// 使用mqtt.js库创建MQTT连接 // 创建MQTT客户端实例
const clientId = 'client_' + Math.random().toString(36).substr(2, 9);
// 使用WebSocket连接方式
this.mqttClient = new Paho.Client('13.39.200.14', 8083, clientId);
// 设置回调函数
this.mqttClient.onConnectionLost = this.onConnectionLost;
this.mqttClient.onMessageArrived = this.onMessageArrived;
// 连接选项
const options = { const options = {
clientId: 'client_' + Math.random().toString(36).substr(2, 9), onSuccess: this.onConnect,
username: 'admin', onFailure: this.onFail,
userName: 'admin',
password: 'zzkj@688737', password: 'zzkj@688737',
keepalive: 60, useSSL: false,
clean: true, timeout: 30,
reconnectPeriod: 5000, keepAliveInterval: 60,
connectTimeout: 30 * 1000 cleanSession: true
}; };
// 使用WebSocket连接到MQTT broker // 连接到MQTT broker
const brokerURL = 'ws://13.39.200.14:8083/mqtt'; this.mqttClient.connect(options);
this.mqttClient = mqtt.connect(brokerURL, options); },
onConnect() {
// 连接成功 console.log('Connected to MQTT broker!');
this.mqttClient.on('connect', () => { // 订阅主题
console.log('MQTT连接成功!'); this.mqttClient.subscribe('WJ_Get_NewControlSystem');
// 订阅主题 // 发送获取数据的消息
this.mqttClient.subscribe('WJ_Get_NewControlSystem', (err) => { this.sendMQTTMessage('WJ_Get_NewControlSystem', JSON.stringify({
if (err) { fun: 'GET',
console.log('订阅主题失败:', err); type: 'WJ_Get_NewControlSystem',
} else { content: 0
console.log('订阅主题成功: WJ_Get_NewControlSystem'); }));
// 发送获取数据的消息 },
this.sendMQTTMessage('WJ_Get_NewControlSystem', JSON.stringify({ onFail(error) {
fun: 'GET', console.error('Failed to connect:', error);
type: 'WJ_Get_NewControlSystem', },
content: 0 onConnectionLost(responseObject) {
})); if (responseObject.errorCode !== 0) {
} console.log('Connection lost: ' + responseObject.errorMessage);
}); }
}); },
onMessageArrived(message) {
// 连接错误 console.log('Message arrived:', message.payloadString);
this.mqttClient.on('error', (err) => { try {
console.error('MQTT连接错误:', err); // let data = JSON.parse(message.payloadString);
}); // if (data.type == 'WJ_Get_NewControlSystem') {
// this.control(data.content);
// 连接关闭 // }
this.mqttClient.on('close', () => { } catch (error) {
console.log('MQTT连接关闭'); console.log('解析MQTT消息错误:', error);
}); }
// 重连
this.mqttClient.on('reconnect', () => {
console.log('MQTT正在重连...');
});
// 收到消息
this.mqttClient.on('message', (topic, message) => {
console.log('收到MQTT消息:', topic, message.toString());
try {
// let data = JSON.parse(message.toString());
// if (data.type == 'WJ_Get_NewControlSystem') {
// this.control(data.content);
// }
} catch (error) {
console.log('解析MQTT消息错误:', error);
}
});
}, },
sendMQTTMessage(topic, payload) { sendMQTTMessage(topic, payload) {
if (this.mqttClient && this.mqttClient.connected) { if (this.mqttClient && this.mqttClient.isConnected()) {
this.mqttClient.publish(topic, payload); const message = new Paho.Message(payload);
console.log('MQTT消息发送成功:', topic, payload); message.destinationName = topic;
this.mqttClient.send(message);
console.log('MQTT消息发送成功:', payload);
} else { } else {
console.log('MQTT客户端未连接'); console.log('MQTT客户端未连接');
if (!this.mqttClient) { if (!this.mqttClient) {