我正在尝试为Node-Red创建一个新节点。基本上它是一个udp侦听套接字,应通过配置节点建立,并将所有传入的消息传递给专用节点进行处理。这是我拥有的基本内容:
function udpServer(n) {
RED.nodes.createNode(this, n);
this.addr = n.host;
this.port = n.port;
var node = this;
var socket = dgram.createSocket('udp4');
socket.on('listening', function () {
var address = socket.address();
logInfo('UDP Server listening on ' + address.address + ":" + address.port);
});
socket.on('message', function (message, remote) {
var bb = new ByteBuffer.fromBinary(message,1,0);
var CoEdata = decodeCoE(bb);
if (CoEdata.type == 'digital') { //handle digital output
// pass to digital handling node
}
else if (CoEdata.type == 'analogue'){ //handle analogue output
// pass to analogue handling node
}
});
socket.on("error", function (err) {
logError("Socket error: " + err);
socket.close();
});
socket.bind({
address: node.addr,
port: node.port,
exclusive: true
});
node.on("close", function(done) {
socket.close();
});
}
RED.nodes.registerType("myServernode", udpServer);
对于处理节点:
function ProcessAnalog(n) {
RED.nodes.createNode(this, n);
var node = this;
this.serverConfig = RED.nodes.getNode(this.server);
this.channel = n.channel;
// how do I get the server's message here?
}
RED.nodes.registerType("process-analogue-in", ProcessAnalog);
我不知道如何将套接字接收的消息传递给可变数量的处理节点,即多个处理节点应在服务器实例上共享。
==== 编辑以更清晰 =====
我想开发一组新的节点:
一个服务器节点:
- 使用配置节点创建 UDP 侦听套接字
- 管理套接字连接(关闭事件、错误等)
- 接收具有一对多个不同数据通道的数据包
一对多处理节点
- 处理节点应共享服务器节点已建立的相同连接 处理
- 节点应处理服务器发出的消息
- 可能 Node-Red 流将使用与服务器数据包中的通道一样多的处理节点
引用有关配置节点的 Node-Red 文档:
配置节点的常见用途是表示与 远程系统。在这种情况下,配置节点也可能是 负责创建连接并将其提供给 使用配置节点的节点。在这种情况下,配置节点应该 还要处理关闭事件以在节点停止时断开连接。
据我了解,我通过this.serverConfig = RED.nodes.getNode(this.server);
使连接可用,但我无法弄清楚如何将此连接接收的数据传递给使用此连接的节点。
节点不知道它连接到下游的节点。
从第一个节点,您可以做的最好的事情是有 2 个输出,并将数字输出发送到一个,将模拟发送到另一个。
您可以通过将数组传递给 node.send()
函数来执行此操作。
例如
//this sends output to just the first output
node.sent([msg,null]);
//this sends output to just the second output
node.send([null,msg]);
接收消息的节点需要添加监听器进行input
例如
node.on('input', function(msg) {
...
});
所有这些都在 Node-RED 页面上有很好的记录
另一种选择是,如果udpServer
节点是配置节点,那么您需要实现自己的侦听器,最好的办法是看起来像核心中的MQTT节点,以显示池连接的示例。