Node节点 .js文件定义了节点运行时的行为。
Node构造函数
Node节点由构造函数定义,该函数用于创建节点的新实例。该函数在运行时注册,以便在流中部署相应类型的节点时可以调用该函数。
function SampleNode(config) {
    RED.nodes.createNode(this,config);
    // node-specific code goes here
 
}
 
RED.nodes.registerType("sample",SampleNode);
接收消息
节点通过注册侦听事件,以接收来自流中上流节点的消息。
this.on('input', function(msg, send, done) {
    // do something with 'msg'
 
    // Once finished, call 'done'.
    // This call is wrapped in a check that 'done' exists
    // so the node will work in earlier versions of Node-RED (<1.0)
    if (done) {
        done();
    }
});
发送消息
如果节点位于流的开始位置并生成消息以响应外部事件,则节点应使用 Node 对象上的函数:send
var msg = { payload:"hi" }
this.send(msg);
如果节点想要从事件侦听器内部发送,以响应接收消息,它应使用传递给侦听器函数的函数:input send
let node = this;
this.on('input', function(msg, send, done) {
    // For maximum backwards compatibility, check that send exists.
    // If this node is installed in Node-RED 0.x, it will need to
    // fallback to using `node.send`
    send = send || function() { node.send.apply(node,arguments) }
 
    msg.payload = "hi";
    send(msg);
 
    if (done) {
        done();
    }
});
关闭节点
每当部署新的流时,都会删除现有节点。如果其中任何一个在发生这种情况时需要整理状态(如断开与远程系统的连接),则需要注册 close监听事件
this.on('close', function() {
    // tidy up any state
});
记录事件
如果节点需要将某些东西记录到控制台,它可以使用以下功能之一:
this.log("Something happened");
this.warn("Something happened you should know about");
this.error("Oh no, something bad happened");
 
// Since Node-RED 0.17
this.trace("Log some internal detail not needed for normal operation");
this.debug("Log something more details for debugging the node's behaviour");
并且 使用warn,error消息也会发送到流编辑器调试选项卡。
© 版权声明
特别提醒: 内容为用户自行发布,如有侵权,请联系我们管理员删除,邮箱:mail@xieniao.com ,在收到您的邮件后我们会在3个工作日内处理。
相关文章
暂无评论...
 
                             
                         
                             
               
                             
                         赣公网安备36020002000448号
 赣公网安备36020002000448号