- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在为 NodeJS 构建一个 C++ 插件,我想将数据从 C++ 异步流式传输到 Node。我找到了这篇文章,https://nodeaddons.com/streaming-data-into-a-node-js-c-addon/ , 然而;我想使用 N-API 而不是 NAN。
我一直在搜索 NodeJS 文档和示例,并寻找其他资源和示例,但没有找到可以告诉我如何完成此操作的资源。这是我第一次为 NodeJS 编写 C++ 插件。
一个可以帮助我入门的示例是一个插件,它使用 N-API 每秒向 Node 发送一个虚拟字符串,Node 会将字符串打印到控制台。
最佳答案
这是一个基于 EventEmitter 概念的片段,它模拟从本地层 (C/C++) 读取传感器并将数据推送到 Node.js (JavaScript) 层。在这个例子中,我们使用了 node-addon-api,一个 N-API 的头文件 C++ 包装器。对于这个例子,我们使用了一个 for 循环(只有五个迭代),实际上它可能是一个无限循环,不断读取传感器输出并将数据推送到 JS 层。 native 层可以决定何时将从传感器收集的数据报告给 JS 层。 JS 将异步接收订阅事件的数据。
#include <napi.h>
#include <thread>
Napi::Value CallEmit(const Napi::CallbackInfo &info)
{
char buff1[128];
char buff2[128];
int sensor1 = 0;
int sensor2 = 0;
Napi::Env env = info.Env();
Napi::Function emit = info[0].As<Napi::Function>();
emit.Call( {Napi::String::New(env, "start")} );
for (int i = 0; i < 5; i++)
{
// Let us simulate some delay for collecting data from its sensors
std::this_thread::sleep_for(std::chrono::seconds(2));
sprintf(buff1, "sensor1 data %d ...", ++sensor1);
emit.Call( { Napi::String::New(env, "sensor1"),
Napi::String::New(env, buff1 ) } );
// Let, sensor 2 data is reported half the rate as sensor1
if (i % 2)
{
sprintf(buff2, "sensor2 data %d ...", ++sensor2);
emit.Call({ Napi::String::New(env, "sensor2"),
Napi::String::New(env, buff2) });
}
}
emit.Call( {Napi::String::New(env, "end")} );
return Napi::String::New( env, "OK" );
}
模块注册片段是
#include <napi.h>
Napi::Object Init( Napi::Env env, Napi::Object exports )
{
exports.Set(Napi::String::New(env, "callEmit"), Napi::Function::New(env, CallEmit));
return exports;
}
NODE_API_MODULE( myncpp1, Init )
编译上面的原生代码,一旦构建成功,您就可以运行以下 node.js JavaScript 代码来测试它。
'use strict'
const EventEmitter = require('events').EventEmitter
const addon = require('bindings')('myncpp1')
// General theme of EventEmitter is: notify me when it is ready
function Main() {
const emitter = new EventEmitter()
emitter.on('start', () => {
console.log( '### Sensor reading started ...');
})
emitter.on('sensor1', (evt) => {
// This module will be called as on when the
// sensor1 data available for consuming from JS
console.log(evt);
})
emitter.on('sensor2', (evt) => {
console.log(evt);
})
emitter.on('end', () => {
console.log('### Sensor reading Ended');
})
addon.callEmit( emitter.emit.bind(emitter) )
}
Main();
代码片段应产生以下输出。
### Sensor reading started ...
sensor1 data 1 ...
sensor1 data 2 ...
sensor2 data 1 ...
sensor1 data 3 ...
sensor1 data 4 ...
sensor2 data 2 ...
sensor1 data 5 ...
### Sensor reading Ended
关于c++ - 使用 N-API 将数据流式传输到 Node.js C++ 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55637772/
是否可以插入到初始表,然后使用插入的 ID 插入到主表中,该主表在一个数据流的列之间具有外键约束? 我是集成服务的新手,不知道这些功能 场景: 表 A - ID - DESC 表 B - ID - A
在 Azure 数据流中,在聚合转换中是否可以在分组依据中动态包含列?我在分组依据中可能需要 8 列,具体取决于它们的值,即如果值为 1,则包含在分组依据中。 简化为 2 列: Column1
我想要实现的是在azure数据流中包含错误处理,如果在传输行时发生错误,它不应该失败,它会处理其他行并将发生错误的行的ID保存在文本文件或日志中 示例: 假设我们有 10 行要沉入表中,不知何故我们在
我的数据流作业将源和接收器作为突触数据库。 我在从突触数据库提取数据时有一个源查询,其中包含数据流中的联接和转换。 众所周知,底层的数据流将启动 databricks 集群来执行数据流代码。 我的问题
这是关于非常常见的传感器数据处理问题。 为了同步和合并来自不同来源的传感器数据,我想用 Java 实现它,而不需要太复杂的第三个库或框架。 假设我定义了一个对象 (O),它由 4 个属性 (A1,..
我开始从事一个项目,我需要使用 PowerTrack/GNIP 流式传输 Twitter 数据,老实说,我在网络方面非常非常缺乏经验,而且我完全不了解网络方面的知识到数据流 (HTTP),它们如何工作
我有一个后端要用 Python 实现,它应该将数据流式传输到 JavaScript 正在创建表示的 Web 浏览器(例如,不断更新变量或绘制到 )。 该数据将以高达 100 Hz 的速率更新(最坏情
我构建了一个简单的 MERN 应用程序,用户可以在其中对电话号码进行评分。用户只需填写电话号码,选择评级(1 - 5 星评级)、城市和短文本。该应用程序具有带过滤和排序选项的搜索功能。这一切都足够好
我在 TPL 数据流上使用顺序管道构建,它由 3 个块组成: B1 - 准备消息 B2 - 将消息发布到远程服务 B3 - 保存结果 问题是如何在发生服务关闭等错误时关闭管道。管道必须以受控方式关闭,
我在 ADF 数据流中有一个数据集(ADLS Gen2 中存在的 csv 文件)。我第一次尝试进行数据预览时,原始文件中的所有列都正确显示。然后,我从 csv 文件中删除了第一列并刷新了“数据预览”选
我正在使用 ADF v2 DataFlow ativity 将数据从 Blob 存储中的 csv 文件加载到 Azure SQL 数据库中的表中。在数据流(源 - Blob 存储)中,在源选项中,有一
我有很多带有嵌套列表的 json 文件需要展平。问题是它们是不同的,我不想为它们每一个创建一个分支。如何通过输入参数动态执行具有“展开依据”和“输入列”字段的展平事件? 谢谢! 最佳答案 对于展开方式
我一直在尝试使用 Azure 数据工厂的数据流在文件的小数列中进行数据类型检查,但它没有按预期工作。我的问题如下: 我想检查数字 121012132.12 是否为小数,因此我使用数据流的派生列并编写表
我们使用 Azure 数据流在 Azure SQL 数据仓库中生成数据表的历史记录。在数据流中,我们在所有列上使用 md5 或 sha1 函数来生成唯一的行指纹来检测记录中的更改,或识别已删除/新记录
我们使用 Azure 数据流在 Azure SQL 数据仓库中生成数据表的历史记录。在数据流中,我们在所有列上使用 md5 或 sha1 函数来生成唯一的行指纹来检测记录中的更改,或识别已删除/新记录
我之前使用 bz2 来尝试解压缩输入。我想要解码的输入已经是压缩格式,因此我决定将格式输入到交互式 Python 控制台中: >>> import bz2 >>> bz2.decompress(inp
在测试 WPF 项目中,我尝试使用 TPL 数据流来枚举给定父目录的所有子目录,并创建具有特定文件扩展名的文件列表,例如“.xlsx”。我使用 2 个 block ,第一个 dirToFilesBlo
问题:为什么使用 WriteOnceBlock (或 BufferBlock )用于从另一个 BufferBlock 取回答案(类似回调) (取回答案发生在发布的 Action 中)导致死锁(在此代码
此代码永远不会到达最后一行,因为完成不会从 saveBlock 传播到 sendBlock。我做错了什么? var readGenerateBlock = new TransformBlock(n =
好吧,我知道我的问题需要更多的指导,而不是技术细节,但我希望 SO 成员不会介意 TPL 数据流的新手提出一些非常基础的问题。 我有一个简单的 Windows 窗体应用程序,它负责从我系统上的 Exc
我是一名优秀的程序员,十分优秀!