本文介绍了发送配置数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在配置时向节点发送异步数据。我想
执行一个SQL请求来列出一些数据。




  • 在节点创建时,执行服务器端功能

  • 完成后,回调将数据发送到节点配置

  • 在节点配置中,收到数据时,列表已创建



或者,二进制文件可以每隔x分钟请求一次数据库并创建每个节点在创建时将使用的
缓存,这将删除异步
部分代码,即使它不再实时更新。



事实上,我被卡住了,因为我创建了查询并将其添加为下面:

  module.exports = function(RED){
use strict;
var db = require(../ bin / database)(RED);

函数testNode(n){
//创建一个RED节点
RED.nodes.createNode(this,n);

//存储节点配置的本地副本(如
.html
var node = this;
var context = this.context();


this.on('input',function(msg){
node.send({payload:true});
});
}

RED.nodes.registerType(SQLTEST,testNode);
}

但是我不知道如何将数据传递给配置节点。我想到了
Socket.IO这样做,但是,这是一个好主意吗?它可用吗?知道任何解决方案吗?

解决方案

Node-RED中使用的标准模型是节点注册自己的admin http端点,用于查询所需的信息。您可以通过串行节点查看此信息。



串行节点编辑对话框列出了当前连接的串行设备供您选择来自。



节点向她注册管理端点e:

  RED.httpAdmin.get(/ serialports,RED.auth.needsPermission('serial.read'),function(req,res){
serialp.list(function(错误,端口){
res.json(ports);
});
});

关键点:




  • 选择一个命名空间为您的节点类型的网址 - 这可以避免冲突

  • needsPermission 中间件只能确保经过身份验证的用户可以访问端点。权限应为< node-type> .read



https://github.com/node-red/node-red-nodes/blob/83ea35d0ddd70803d97ccf488d675d6837beeceb/io/serialport/25-serial.html#L240

  $。getJSON('serialports',function(data){
// ...确实包含数据
} );

关键点:




  • 此处的网址不得以 / 开头。这样可以确保相对于编辑器所在的位置发出请求 - 您无法假设它是从 / 提供的。


I want to send asynchronous data to the node on configuration. I want to perform a SQL request to list some data in a .

  • On node creation, a server side function is performed
  • When it's done, a callback send data to the node configuration
  • On node configuration, when data is received, the list is created

Alternatively, the binary can request database each x minutes and create a cache that each node will use on creation, this will remove the asynchronous part of code, even if it's no longer "live updated".

In fact, i'm stuck because i created the query and added it as below :

module.exports = function(RED) {
    "use strict";
    var db = require("../bin/database")(RED);

    function testNode(n) {
        // Create a RED node
        RED.nodes.createNode(this,n);

        // Store local copies of the node configuration (as defined in the 
.html
        var node = this;
        var context = this.context();


        this.on('input', function (msg) {
            node.send({payload: true});
        });
    }

    RED.nodes.registerType("SQLTEST",testNode);
}

But I don't know how to pass data to the configuration node. I thought of Socket.IO to do it, but, is this a good idea and is it available? Do you know any solution ?

解决方案

The standard model used in Node-RED is for the node to register its own admin http endpoint that can be used to query the information it needs. You can see this in action with the Serial node.

The Serial node edit dialog lists the currently connected serial devices for you to pick from.

The node registers the admin endpoint here: https://github.com/node-red/node-red-nodes/blob/83ea35d0ddd70803d97ccf488d675d6837beeceb/io/serialport/25-serial.js#L283

RED.httpAdmin.get("/serialports", RED.auth.needsPermission('serial.read'), function(req,res) {
    serialp.list(function (err, ports) {
        res.json(ports);
    });
});

Key points:

  • pick a url that is namespaced to your node type - this avoids clashes
  • the needsPermission middleware is there to ensure only authenticated users can access the endpoint. The permission should be of the form <node-type>.read.

Its edit dialog then queries that endpoint from here: https://github.com/node-red/node-red-nodes/blob/83ea35d0ddd70803d97ccf488d675d6837beeceb/io/serialport/25-serial.html#L240

$.getJSON('serialports',function(data) {
    //... does stuff with data
});

Key points:

  • here the url must not begin with a /. That ensures the request is made relative to wherever the editor is being served from - you cannot assume it is being served from /.

这篇关于发送配置数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 14:14