c++ - Node.js 在 C/C++ 中添加模块
问题描述
我一直在探索用 C/C++ 为 node.js 编写附加模块。到目前为止,我有一个简单的附加工作,我可以在 JavaScript 中调用附加功能,这非常简单。该例程称为hello,它传递一个字符串,然后返回以Hello为前缀的相同字符串,两个单词之间有一个空格。
包.json:
{
"name": "test",
"version": "2.0.0",
"description": "test description",
"main": "index.js",
"gypfile": true,
"scripts": {
"build": "node-gyp rebuild",
"clean": "node-gyp clean"
},
"author": "Simon Platten",
"license": "ISC",
"devDependencies": {
"node-gyp": "^7.1.2"
},
"dependencies": {
"node-addon-api": "^3.1.0"
}
}
绑定.gyp:
{
"targets":[{
"target_name": "test",
"cflags!": ["-fno-exceptions"],
"cflags_cc!": ["-fno-exceptions"],
"sources": ["cpp/main.cpp"],
"include_dirs": ["<!@(node -p \"require('node-addon-api').include\")"],
"libraries": [],
"dependencies": ["<!(node -p \"require('node-addon-api').gyp\")"],
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"],
}]
}
主文件
/**
* File: main.cpp
* Notes:
* Work in progress for add on module for node.js 'test'
* History:
* 2021/03/11 Written by Simon Platten
*/
#include <napi.h>
#include <iostream>
#include <string>
/**
* Return "Hello " + user name passed back
*/
Napi::String hello(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string strResult = "Hello ";
strResult += info[0].ToString();
return Napi::String::New(env, strResult);
}
/**
* Callback method when module is registered with Node.js
*/
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(
Napi::String::New(env, "hello"),
Napi::Function::New(env, hello)
);
return exports;
}
NODE_API_MODULE(test, Init);
index.js:
const test = require("./build/Release/test");
console.log(test.hello("Simon"));
这很好用,现在我想更冒险一点。我不确定正确的术语,但是例如,当在节点中使用套接字时,可以使用回调函数管理各种on回调或事件。这正是我想从我的模块中实现的,模块将传递大量数据进行处理,当模块处理数据时,我将立即返回,并且当它完成并准备好用于我要发布的客户端时以on样式处理程序的形式向 node.js 发送通知。
如何?
解决方案
在 JS 中:
创建一个继承自EventEmitter的类。这样做你就可以收听这个类的事件。
如果您使用此类的回调调用插件的函数,并且在回调中发出事件,则可以实现您想要的。
像这样的东西:
const test = require("./build/Release/test");
const EventEmitter = require('events');
class Test extends EventEmitter {
constructor() {
super();
this.addon = test;
}
test() {
test.hello("Simon", (data) => {
this.emit("testEvent", data);
});
}
}
const myTest = new Test();
myTest.on("testEvent", (data) => {
console.log("testEvent says: ", data);
})
myTest.test();
在 C++ 中:
根据此处找到的示例,您可以添加一个 AsyncWorker,它将在后台处理您的数据并在完成后调用回调。
像这样:
#include <napi.h>
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
using namespace Napi;
class TestWorker : public AsyncWorker {
public:
TestWorker(Function& callback) : AsyncWorker(callback) {}
void Execute() override {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
void OnOK() override {
HandleScope scope(Env());
Callback().Call({String::New(Env(), "message from the addon")});
}
};
您可以将此回调作为参数提供给您的 hello 函数以及当前参数“Simon”。
您的代码使用回调参数和 AsyncWorker 的开头进行了扩展:
Napi::String hello(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string strResult = "Hello ";
strResult += info[0].ToString();
Function cb = info[1].As<Function>();
TestWorker* wk = new TestWorker(cb);
wk->Queue();
return Napi::String::New(env, strResult);
}
推荐阅读
- javascript - 如何从常量中解析变量名?
- kubernetes - FATA [0004] 无法初始化项目:无法使用“base.go.kubebuilder.io/v3”搭建脚手架:退出状态 2
- android - 为自定义 tabList 视图添加 contentDescription
- jestjs - jest 是否支持代理外部脚本,例如
- javascript - 如何在按钮单击时更改菜单?
- python - 在主线程中执行一个while循环,直到我们从辅助线程中调用的函数获得返回值
- drupal-9 - Drupal 站点问题
- itfoxtec-identity-saml2 - 支持范围和 IDPList
- docker-compose - docker-compose:根据环境变量启动容器 A 或 B
- java - java set如何判断类似的ListNode