diff --git a/app/src/main/mqtt-manager.js b/app/src/main/mqtt-manager.js index 7c62636..a296c3c 100644 --- a/app/src/main/mqtt-manager.js +++ b/app/src/main/mqtt-manager.js @@ -10,11 +10,17 @@ class MqttManager { this.messages = new Map() // serverId -> message[] this.publishHistory = new Map() // serverId -> publishHistory[] this.listeners = new Set() // IPC callback functions + this.msgIdCounter = 0 // 用于生成唯一消息 id this.dataPath = join(app.getPath('userData'), 'frost-mqtt-data.json') this.loadData() } + nextMsgId() { + this.msgIdCounter = (this.msgIdCounter + 1) % Number.MAX_SAFE_INTEGER + return 'msg_' + Date.now() + '_' + this.msgIdCounter + } + // ==================== 数据持久化 ==================== getDataDir() { const dir = app.getPath('userData') @@ -269,9 +275,9 @@ class MqttManager { qos: packet.qos, retain: packet.retain, direction: 'sub', - id: 'msg_' + Date.now() - } - const msgs = this.messages.get(id) || [] + id: this.nextMsgId() + } + const msgs = this.messages.get(id) || [] msgs.unshift(msg) if (msgs.length > 500) msgs.pop() this.messages.set(id, msgs) @@ -479,10 +485,11 @@ class MqttManager { this.publishHistory.set(serverId, history) // 同时添加到消息日志 + const pubMsgId = this.nextMsgId() const msgs = this.messages.get(serverId) || [] msgs.unshift({ ...pubEntry, - id: 'msg_' + Date.now() + id: pubMsgId }) if (msgs.length > 500) msgs.pop() this.messages.set(serverId, msgs) @@ -491,7 +498,7 @@ class MqttManager { serverId, message: { ...pubEntry, - id: 'msg_' + Date.now() + id: pubMsgId } })