2016-07-15 32 views
0

我試圖爲節點紅色創建一個新節點。基本上它是一個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監聽套接字
  • 管理套接字連接(接近事件,錯誤等)
  • RECE艾夫斯的數據包與一個不同的數據的多渠道

一對多處理節點

  • 的處理節點應共享同一連接的服務器節點建立
  • 的處理節點應處理服務器已發射
  • 消息可能是由於有在服務器的數據包信道節點 - 紅色流將使用盡可能多的處理節點

引述上配置節點的節點紅色文檔:

配置節點的一個常見用途是表示到遠程 系統中的共享連接。在這種情況下,配置節點也可能是 負責創建連接並使其可到使用配置節點的 節點。在這種情況下,配置節點 也會處理關閉事件,以便在節點停止時斷開連接。

據我理解這一點,我使經由this.serverConfig = RED.nodes.getNode(this.server);可用的連接,但我不能弄清楚如何傳遞數據,這是由該連接接收,到使用此連接的節點。

回答

2

一個節點有沒有哪些節點被連接到下游的知識。

,你可以從第一個節點做的最好的是有2個輸出和發送數字到一個和模擬到其他。

你會被傳遞一個數組到node.send()功能做到這一點。

E.g.

//this sends output to just the first output 
node.sent([msg,null]); 

//this sends output to just the second output 
node.send([null,msg]); 

節點具有接收messagess需要添加一個偵聽input

例如

node.on('input', function(msg) { 
    ... 
}); 

所有這一切是有據可查的節點RED page

另一種選擇是,如果udpServer節點是配置節點,那麼你需要實現自己的聽衆,最好的辦法是看起來像核心中的MQTT節點,用於連接池的示例

+0

非常感謝您的回答。我想我的最初問題還不夠精確,因爲我想實現不同的目標。爲了更加清晰,我編輯了我最初的問題(我希望) – Tom

+0

因此,這適合我描述的第二種選擇,您應該查看核心節點中的MQTT節點https://github.com/node-red/node-red/斑點/主/節點/核心/ IO/10-mqtt.js – hardillb