对接MQTT、APP样式
This commit is contained in:
@ -8,7 +8,12 @@ mqtturl = "ws://122.51.194.184:8083/mqtt";
|
||||
import * as mqtt from "mqtt/dist/mqtt.min.js";
|
||||
// #endif
|
||||
|
||||
// #ifdef APP-PLUS || MP-WEIXIN
|
||||
// #ifdef APP-PLUS
|
||||
mqtturl = "wx://122.51.194.184:8083/mqtt";
|
||||
import * as mqtt from "mqtt/dist/mqtt.min.js";
|
||||
//#endif
|
||||
|
||||
// #ifdef MP-WEIXIN
|
||||
mqtturl = "wx://122.51.194.184:8083/mqtt";
|
||||
import * as mqtt from "mqtt/dist/mqtt.min.js";
|
||||
//#endif
|
||||
@ -35,9 +40,10 @@ const createMqtt = () => {
|
||||
password: "qwer1234",
|
||||
protocolVersion: 4,
|
||||
clean: true,
|
||||
reconnectPeriod: 1000, // reconnectPeriod为1000毫秒,这意味着在连接丢失之后,客户端将在1秒后尝试重新连接。
|
||||
connectTimeout: 5000, // 5s超时时间 意味着mqtt-reconnect函数5秒钟触发一次
|
||||
topic: "HDYDCJ_01_DOWN",
|
||||
reconnectPeriod: 1000, // 恢复自动重连,1秒重连一次
|
||||
connectTimeout: 5000, // 5s超时时间
|
||||
// topic: "HDYDCJ_01_DOWN",
|
||||
topic: "HDYDCJ_01_UP",
|
||||
rejectUnauthorized: false,
|
||||
// #ifdef MP-ALIPAY
|
||||
my: my,//注意这里的my
|
||||
@ -49,12 +55,44 @@ const createMqtt = () => {
|
||||
console.log('🔧 开始创建MQTT连接...');
|
||||
console.log('🔧 MQTT URL:', mqtturl);
|
||||
console.log('🔧 连接选项:', options);
|
||||
console.log('🔧 当前平台:',
|
||||
// #ifdef H5
|
||||
'H5'
|
||||
// #endif
|
||||
// #ifdef APP-PLUS
|
||||
'APP-PLUS'
|
||||
// #endif
|
||||
// #ifdef MP-WEIXIN
|
||||
'MP-WEIXIN'
|
||||
// #endif
|
||||
// #ifdef MP-ALIPAY
|
||||
'MP-ALIPAY'
|
||||
// #endif
|
||||
)
|
||||
|
||||
// 显示连接loading
|
||||
uni.showLoading({
|
||||
title: 'MQTT连接中...',
|
||||
mask: true
|
||||
});
|
||||
|
||||
client = mqtt.connect(mqtturl, options);
|
||||
initEventHandleMqtt(options.topic);
|
||||
} else {
|
||||
console.log('🔧 MQTT客户端已存在,跳过创建');
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('❌ MQTT连接创建失败:', e);
|
||||
console.error('❌ 错误详情:', e.message);
|
||||
console.error('❌ 错误堆栈:', e.stack);
|
||||
|
||||
// 连接失败时隐藏loading
|
||||
uni.hideLoading();
|
||||
uni.showToast({
|
||||
title: 'MQTT连接失败' + mqtturl,
|
||||
icon: 'error',
|
||||
duration: 3000
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@ -64,10 +102,23 @@ const initEventHandleMqtt = (topicUrl) => {
|
||||
client.on("connect", function() {
|
||||
uni.hideLoading();
|
||||
console.log("✅ MQTT连接成功");
|
||||
|
||||
// 显示连接成功提示
|
||||
uni.showToast({
|
||||
title: 'MQTT连接成功' + mqtturl,
|
||||
icon: 'success',
|
||||
duration: 2000
|
||||
});
|
||||
|
||||
//订阅主题
|
||||
client.subscribe(topicUrl, function(err) {
|
||||
if (err) {
|
||||
console.error("❌ MQTT订阅主题失败:", err);
|
||||
uni.showToast({
|
||||
title: '订阅主题失败',
|
||||
icon: 'error',
|
||||
duration: 3000
|
||||
});
|
||||
} else {
|
||||
console.log("✅ MQTT订阅主题成功:", topicUrl);
|
||||
}
|
||||
@ -84,6 +135,18 @@ const initEventHandleMqtt = (topicUrl) => {
|
||||
// 获取信息
|
||||
const mqttData = JSON.parse(message.toString());
|
||||
console.log('📋 解析后的数据:', mqttData);
|
||||
console.log('数据类型:', Array.isArray(mqttData) ? '数组' : '对象');
|
||||
|
||||
// 如果是数组,打印数组信息
|
||||
if (Array.isArray(mqttData)) {
|
||||
console.log('📋 数组长度:', mqttData.length);
|
||||
mqttData.forEach((item, index) => {
|
||||
console.log(`📦 数组[${index}]:`, item);
|
||||
if (item.Device) {
|
||||
console.log(`🔍 设备类型[${index}]: ${item.Device}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 传递信息
|
||||
uni.$emit("mqttData", mqttData);
|
||||
@ -97,13 +160,14 @@ const initEventHandleMqtt = (topicUrl) => {
|
||||
client.on('reconnect', function() {
|
||||
console.log('🔄 MQTT重新连接中...');
|
||||
uni.showLoading({
|
||||
title: "重新连接"
|
||||
title: "重新连接中..."
|
||||
});
|
||||
});
|
||||
|
||||
// 当客户端无法成功连接时或发生解析错误时触发,参数 error 为错误信息
|
||||
client.on("error", function(err) {
|
||||
console.error('❌ MQTT连接错误:', err);
|
||||
uni.hideLoading();
|
||||
uni.showToast({
|
||||
title: 'MQTT连接错误',
|
||||
icon: 'error',
|
||||
@ -114,16 +178,19 @@ const initEventHandleMqtt = (topicUrl) => {
|
||||
// 在收到 Broker 发送过来的断开连接的报文时触发
|
||||
client.on('disconnect', function() {
|
||||
console.log('⚠️ MQTT连接断开');
|
||||
uni.hideLoading();
|
||||
});
|
||||
|
||||
// 在断开连接以后触发
|
||||
client.on("close", function() {
|
||||
console.log('🔌 MQTT连接关闭');
|
||||
uni.hideLoading();
|
||||
});
|
||||
|
||||
// 当客户端下线时触发
|
||||
client.on("offline", function() {
|
||||
console.log('📴 MQTT客户端离线');
|
||||
uni.hideLoading();
|
||||
});
|
||||
};
|
||||
|
||||
@ -131,6 +198,7 @@ const initEventHandleMqtt = (topicUrl) => {
|
||||
const closeMqtt = () => {
|
||||
if (client) {
|
||||
console.log('🔌 强制断开MQTT连接');
|
||||
uni.hideLoading();
|
||||
client.end();
|
||||
client = null;
|
||||
}
|
||||
@ -150,7 +218,28 @@ const judgeBeat = () => {
|
||||
|
||||
// 获取连接状态
|
||||
const getConnectionStatus = () => {
|
||||
return client && client.connected;
|
||||
if (!client) {
|
||||
console.log('🔍 连接状态检查: 客户端不存在');
|
||||
return false;
|
||||
}
|
||||
|
||||
const isConnected = client.connected;
|
||||
console.log('🔍 连接状态检查:', {
|
||||
clientExists: !!client,
|
||||
connected: isConnected,
|
||||
readyState: client.stream ? client.stream.readyState : 'unknown'
|
||||
});
|
||||
|
||||
return isConnected;
|
||||
};
|
||||
|
||||
// 手动重连函数
|
||||
const manualReconnect = () => {
|
||||
console.log('🔄 手动触发重连');
|
||||
closeMqtt();
|
||||
setTimeout(() => {
|
||||
createMqtt();
|
||||
}, 1000);
|
||||
};
|
||||
|
||||
export {
|
||||
@ -158,5 +247,6 @@ export {
|
||||
closeMqtt,
|
||||
judgeBeat,
|
||||
getConnectionStatus,
|
||||
manualReconnect,
|
||||
client,
|
||||
}
|
||||
Reference in New Issue
Block a user