我想通过单击按钮启动我的 Node ,这样我就不必将注入(inject) Node 放在前面。如何在 javascript 文件中注册按钮点击?
我尝试将 node.on("input", async function(msg){/*some code*/})
放入我注册 Node 的 javascript 文件中。我能够添加此按钮 通过这个:
//HTML file script
<script type="text/javascript">
RED.nodes.registerType('light', {
category: "input",
color: "#f3c12b",
defaults: {
name: {value: ""},
plus: {value: ""},
topic: {value: this.name},
payload: {value: ""}
},
inputs: 0,
outputs: 1,
label: function(){
return "Licht '"+this.name+"'" || "Licht";
},
button: {
enabled: function(){
return true;
},
onclick: function(){
//I´ve put the code here, but then I have to reconfigure my functions
}
}
});
</script>
//Javascript file --> register function
//Not getting any response
node.on("input", async function(msg) {
msg = {};
msg.topic = this.topic;
msg.payload = "This is a new message!";
node.send(msg);
});
我原以为,当我单击此 Node 时, Node 正在发送消息,但 Node 没有任何响应。
最佳答案
这里最好的办法是查看注入(inject) Node 源代码。 inject.html inject.js
在注入(inject) Node 的情况下,HTML 文件中按钮参数的 onclick 函数实际上对服务器上的 /inject/{id}
执行 POST 调用。
onclick: function() {
...
var node = this;
$.ajax({
url: "inject/"+this.id,
type:"POST",
success: function(resp) { ... }
});
}
在服务器上运行的注入(inject) JS 文件在 /inject/:id
托管一个 http 端点,当它被调用时通过 id 获取 Node 并调用 node.receive()
作为其输入的触发器。
module.exports = function(RED) {
...
RED.httpAdmin.post("/inject/:id", RED.auth.needsPermission("inject.write"), function(req,res) {
var node = RED.nodes.getNode(req.params.id);
...
node.receive();
...
});
}
关于javascript - 如何在 NodeRED 中创建自定义注入(inject) Node ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57148641/