【发布时间】:2016-07-16 05:32:23
【问题描述】:
我正在尝试为 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);
我不知道如何将套接字接收到的消息传递给可变数量的处理节点,即多个处理节点应在服务器实例上共享。
==== 编辑更清楚 =====
我想开发一组新的节点:
一个服务器节点:
- 使用 config-node 创建 UDP 监听套接字
- 管理套接字连接(关闭事件、错误等)
- 接收具有一对多通道不同数据的数据包
一对多处理节点
- 处理节点应共享服务器节点已建立的相同连接
- 处理节点应处理服务器发出的消息
- Node-Red 流可能会使用与服务器数据包中的通道一样多的处理节点
引用关于配置节点的 Node-Red 文档:
配置节点的一个常见用途是表示到一个共享连接 远程系统。在这种情况下,配置节点也可能是 负责创建连接并使其可用 使用配置节点的节点。在这种情况下,配置节点应该 节点停止时也处理关闭事件断开连接。
据我了解,我通过this.serverConfig = RED.nodes.getNode(this.server); 使连接可用,但我不知道如何将由此连接接收到的数据传递到使用此连接的节点。
【问题讨论】: