node.js - Node-Red: Create server and share input -
i'm trying create new node node-red. udp listening socket shall established via config node , shall pass incoming messages dedicated nodes processing. basic have:
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 digital handling node } else if (coedata.type == 'analogue'){ //handle analogue output // pass 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);
for processing node:
function processanalog(n) { red.nodes.createnode(this, n); var node = this; this.serverconfig = red.nodes.getnode(this.server); this.channel = n.channel; // how server's message here? } red.nodes.registertype("process-analogue-in", processanalog);
i can't figure out how pass messages socket receives variable number of processing nodes, i.e. multiple processing nodes shall share on server instance.
==== edit more clarity =====
i want develop new set of nodes:
one server node:
- uses config-node create udp listening socket
- managing socket connection (close events, error etc)
- receives data packages 1 many channels of different data
one many processing nodes
- the processing nodes shall share same connection server node has established
- the processing nodes shall handle messages server emitting
- possibly node-red flow use many processing nodes there channels in server's data package
to quote node-red documentation on config-nodes:
a common use of config nodes represent shared connection remote system. in instance, config node may responsible creating connection , making available nodes use config node. in such cases, config node should handle close event disconnect when node stopped.
as far understood this, make connection available via this.serverconfig = red.nodes.getnode(this.server);
cannot figure out how pass data, received connection, node using connection.
a node has no knowledge of nodes connected downstream.
the best can first node have 2 outputs , send digital 1 , analogue other.
you passing array node.send()
function.
e.g.
//this sends output first output node.sent([msg,null]); //this sends output second output node.send([null,msg]);
nodes have receive messagess need add listener input
e.g.
node.on('input', function(msg) { ... });
all of documented on node-red page
the other option if udpserver
node config node need implement own listeners, best bet mqtt nodes in core examples of pooling connections
Comments
Post a Comment