rol-websocket-channel 1.7.0 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import * as os from "os";
|
|
3
4
|
// ─────────────────────────────────────────────
|
|
4
5
|
// 全局单例连接
|
|
5
6
|
// ─────────────────────────────────────────────
|
|
@@ -58,13 +59,37 @@ export function getSubscribeTopic(topic) {
|
|
|
58
59
|
return topic;
|
|
59
60
|
}
|
|
60
61
|
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
62
|
+
* 获取持久化的 MQTT 客户端 ID
|
|
63
|
+
* 第一次生成后会保存在 ~/.openclaw/mqtt_client_id.txt 中,后续重启保持不变
|
|
63
64
|
*/
|
|
64
65
|
export function generateClientId() {
|
|
66
|
+
const configPath = path.join(os.homedir(), ".openclaw", "mqtt_client_id.txt");
|
|
67
|
+
try {
|
|
68
|
+
if (fs.existsSync(configPath)) {
|
|
69
|
+
const savedId = fs.readFileSync(configPath, "utf-8").trim();
|
|
70
|
+
if (savedId) {
|
|
71
|
+
return savedId;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
// 忽略读取错误
|
|
77
|
+
}
|
|
78
|
+
// 若文件不存在,则生成新 ID
|
|
65
79
|
const rand = Math.random().toString(36).slice(2, 10).padEnd(8, "0");
|
|
66
80
|
const ts = Date.now().toString(36);
|
|
67
|
-
|
|
81
|
+
const newId = `mqtt_client_${rand}${ts}`;
|
|
82
|
+
try {
|
|
83
|
+
const dir = path.dirname(configPath);
|
|
84
|
+
if (!fs.existsSync(dir)) {
|
|
85
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
86
|
+
}
|
|
87
|
+
fs.writeFileSync(configPath, newId, "utf-8");
|
|
88
|
+
}
|
|
89
|
+
catch (err) {
|
|
90
|
+
// 忽略写入错误
|
|
91
|
+
}
|
|
92
|
+
return newId;
|
|
68
93
|
}
|
|
69
94
|
// ─────────────────────────────────────────────
|
|
70
95
|
// 全局连接 API
|
package/package.json
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import * as os from "os";
|
|
4
|
+
|
|
1
5
|
// MQTT 全局连接管理器
|
|
2
6
|
// 全局共享单一连接(随机 clientId,从 topic 解析用户名,支持测试注入)
|
|
3
7
|
|
|
@@ -71,13 +75,39 @@ export function getSubscribeTopic(topic: string): string {
|
|
|
71
75
|
}
|
|
72
76
|
|
|
73
77
|
/**
|
|
74
|
-
*
|
|
75
|
-
*
|
|
78
|
+
* 获取持久化的 MQTT 客户端 ID
|
|
79
|
+
* 第一次生成后会保存在 ~/.openclaw/mqtt_client_id.txt 中,后续重启保持不变
|
|
76
80
|
*/
|
|
77
81
|
export function generateClientId(): string {
|
|
82
|
+
const configPath = path.join(os.homedir(), ".openclaw", "mqtt_client_id.txt");
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
if (fs.existsSync(configPath)) {
|
|
86
|
+
const savedId = fs.readFileSync(configPath, "utf-8").trim();
|
|
87
|
+
if (savedId) {
|
|
88
|
+
return savedId;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
} catch (err) {
|
|
92
|
+
// 忽略读取错误
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// 若文件不存在,则生成新 ID
|
|
78
96
|
const rand = Math.random().toString(36).slice(2, 10).padEnd(8, "0");
|
|
79
97
|
const ts = Date.now().toString(36);
|
|
80
|
-
|
|
98
|
+
const newId = `mqtt_client_${rand}${ts}`;
|
|
99
|
+
|
|
100
|
+
try {
|
|
101
|
+
const dir = path.dirname(configPath);
|
|
102
|
+
if (!fs.existsSync(dir)) {
|
|
103
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
104
|
+
}
|
|
105
|
+
fs.writeFileSync(configPath, newId, "utf-8");
|
|
106
|
+
} catch (err) {
|
|
107
|
+
// 忽略写入错误
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return newId;
|
|
81
111
|
}
|
|
82
112
|
|
|
83
113
|
// ─────────────────────────────────────────────
|