feat: 初始版本,支持配置和连接多个 MQTT 服务器,支持配置订阅的主题列表,支持消息历史记录

This commit is contained in:
2026-07-18 19:23:50 +08:00
parent d0bbd32d08
commit 32643c2518
17 changed files with 2931 additions and 34 deletions
+58 -2
View File
@@ -1,8 +1,64 @@
import { contextBridge } from 'electron'
import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'
// Custom APIs for renderer
const api = {}
const api = {
// 服务器管理
getServers: () => ipcRenderer.invoke('mqtt:get-servers'),
addServer: (config) => ipcRenderer.invoke('mqtt:add-server', config),
updateServer: (id, config) => ipcRenderer.invoke('mqtt:update-server', id, config),
deleteServer: (id) => ipcRenderer.invoke('mqtt:delete-server', id),
// 连接管理
connect: (id) => ipcRenderer.invoke('mqtt:connect', id),
disconnect: (id) => ipcRenderer.invoke('mqtt:disconnect', id),
// 主题管理
addTopic: (serverId, topic, qos) => ipcRenderer.invoke('mqtt:add-topic', serverId, topic, qos),
removeTopic: (serverId, topic) => ipcRenderer.invoke('mqtt:remove-topic', serverId, topic),
subscribe: (serverId, topic) => ipcRenderer.invoke('mqtt:subscribe', serverId, topic),
unsubscribe: (serverId, topic) => ipcRenderer.invoke('mqtt:unsubscribe', serverId, topic),
subscribeAll: (serverId) => ipcRenderer.invoke('mqtt:subscribe-all', serverId),
updateTopic: (serverId, topicId, topic, qos) =>
ipcRenderer.invoke('mqtt:update-topic', serverId, topicId, topic, qos),
// 消息发布
publish: (serverId, topic, payload, opts) =>
ipcRenderer.invoke('mqtt:publish', serverId, topic, payload, opts),
// 消息管理
getMessages: (serverId) => ipcRenderer.invoke('mqtt:get-messages', serverId),
getPublishHistory: (serverId) => ipcRenderer.invoke('mqtt:get-publish-history', serverId),
clearMessages: (serverId) => ipcRenderer.invoke('mqtt:clear-messages', serverId),
clearPublishHistory: (serverId) => ipcRenderer.invoke('mqtt:clear-publish-history', serverId),
// 事件监听
onStatusChange: (callback) => {
const handler = (_, data) => callback(data)
ipcRenderer.on('mqtt:status-change', handler)
return () => ipcRenderer.removeListener('mqtt:status-change', handler)
},
onMessage: (callback) => {
const handler = (_, data) => callback(data)
ipcRenderer.on('mqtt:message', handler)
return () => ipcRenderer.removeListener('mqtt:message', handler)
},
onSubscriptionChange: (callback) => {
const handler = (_, data) => callback(data)
ipcRenderer.on('mqtt:subscription-change', handler)
return () => ipcRenderer.removeListener('mqtt:subscription-change', handler)
},
onSubscriptionError: (callback) => {
const handler = (_, data) => callback(data)
ipcRenderer.on('mqtt:subscription-error', handler)
return () => ipcRenderer.removeListener('mqtt:subscription-error', handler)
},
onPublishError: (callback) => {
const handler = (_, data) => callback(data)
ipcRenderer.on('mqtt:publish-error', handler)
return () => ipcRenderer.removeListener('mqtt:publish-error', handler)
}
}
// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise