【发布时间】:2021-12-06 05:09:41
【问题描述】:
我是 electron 和 node.js 的新手,在 mqtt 上工作以更新 DOM。我在 mqtt 上收到消息到我的 main.js onmessage 函数,而我的 main.js 是
const { app, BrowserWindow } = require('electron')
var mqtt = require('mqtt')
var client = mqtt.connect("mqtt://localhost")
client.on("connect", function(){
client.subscribe("testtopic")
})
client.on("message", function(topic, message, packet){
document.getElementById("someId").innerHTML = message
})
function createWindow () {
const win = new BrowserWindow({webPreferences:{nodeIntegration: true}})
win.loadFile('index.html')
win.maximize()
}
app.whenReady().then(() => {
createWindow()
})
我想从我的 onmessage 回调中更新 index.html 中可用的元素。我无法访问它显示错误文档未定义。 如何实现这一点,或者我可以直接在我的 index.html 脚本中导入 mqtt 吗? 在那种情况下,为什么要使用节点 mqtt 而不是我可以使用 paho mqtt。 请对此提出建议。
【问题讨论】:
标签: javascript node.js electron mqtt