feat: 添加后端部署和API基本检索的Docker Compose配置
- 引入新的`docker-compose.yml`文件,便于后端和MQTT代理(EMQX)的部署。 - 更新多个组件中的API基本检索,使用函数进行动态解析。 - 加强了多个组件中API调用的错误处理和日志记录。 - 优化了AI聊天面板离线场景的回退响应。 - 更新DataScreen和MediaScreen组件中的数据显示和统计,以反映准确的指标。 - 重构MQTT连接逻辑,以支持动态凭证和客户端ID。
This commit is contained in:
+19
-6
@@ -1,6 +1,6 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import mqtt from 'mqtt';
|
||||
import { MQTT_URL, MQTT_TOPIC_COMMAND, MQTT_TOPIC_TICK, MQTT_USERNAME, MQTT_PASSWORD } from '../config';
|
||||
import { getMqttUrl, getMqttCredentials, MQTT_TOPIC_COMMAND, MQTT_TOPIC_TICK } from '../config';
|
||||
|
||||
/* =========================================================
|
||||
MQTT 客户端(mqtt.js)—— 后端控制通道
|
||||
@@ -17,18 +17,31 @@ function notifyStatus() {
|
||||
window.dispatchEvent(new CustomEvent('dpm:mqtt-status', { detail: { status } }));
|
||||
}
|
||||
|
||||
/* 每个连接生成全局唯一 client_id(独立客户端 ID 铁律):
|
||||
优先 crypto.randomUUID(WebView2/现代浏览器),回退时间戳+双段高熵随机 */
|
||||
function makeClientId() {
|
||||
try {
|
||||
if (window.crypto && typeof window.crypto.randomUUID === 'function') {
|
||||
return `dpm-screen-${crypto.randomUUID().replace(/-/g, '')}`;
|
||||
}
|
||||
} catch { /* 继续回退 */ }
|
||||
return `dpm-screen-${Date.now().toString(36)}-${Math.random().toString(16).slice(2, 12)}-${Math.random().toString(16).slice(2, 10)}`;
|
||||
}
|
||||
|
||||
export function connectMqtt() {
|
||||
if (client) return;
|
||||
try {
|
||||
// 每个页面会话生成唯一 client_id(时间戳 + 高熵随机),避免多屏/多实例竞争
|
||||
clientId = `dpm-screen-${Date.now().toString(36)}-${Math.random().toString(16).slice(2, 12)}`;
|
||||
client = mqtt.connect(MQTT_URL, {
|
||||
// 独立客户端 ID:避免多屏/多实例/多标签页同 ID 被 broker 互相踢下线
|
||||
clientId = makeClientId();
|
||||
// 连接时惰性读取 MQTT 地址/账号:打包部署时 main.jsx 引导已从后端 /api/config 覆盖
|
||||
const { username, password } = getMqttCredentials();
|
||||
client = mqtt.connect(getMqttUrl(), {
|
||||
reconnectPeriod: 3000,
|
||||
connectTimeout: 8000,
|
||||
clientId,
|
||||
clean: true,
|
||||
username: MQTT_USERNAME || undefined,
|
||||
password: MQTT_PASSWORD || undefined,
|
||||
username: username || undefined,
|
||||
password: password || undefined,
|
||||
});
|
||||
client.on('connect', () => {
|
||||
status = 'connected';
|
||||
|
||||
Reference in New Issue
Block a user