Invoke javascript callback repeatedly from C++ module in node.js

元气小坏坏 提交于 2019-12-23 02:29:35

问题


So I am writing a node.js module in C++ which processes data from a camera. I want to invoke a callback function in my app.js file whenever new data is available.

What I have at the moment

Right now I have in my node javascript file (app.js) the following code. Every second it calls a function in my C++-Module and returns the number of frames that have been processed so far:

setInterval(function () {
        var counter = MyCPPModule.NumberOfFrames();
        console.log(counter);
    }, 1000);

In my C++ file I have the following functions.

1.) I have a function to get the number of frames - the function that gets called from javascript as above- , where frameCounter refers to a global variable.

void NumberOfFrames(const FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = Isolate::GetCurrent();
    HandleScope scope(isolate);
    Local<Integer> retval = v8::Integer::New(isolate, frameCounter);
    args.GetReturnValue().Set(retval);
}

2.) And I have a C++-callback function in the C++ module that gets triggered whenever a new frame is available (just to be clear, this callback is independent of node.js and has nothing directly to do with node.js-callbacks).

void NewFrameAvailable(char** imageBuffer, /* more arguments */)
{
// ... 
     frameCounter++;  // increment global variable
// ...
}

All of this works fine.

What I would like to accomplish

Instead of registering a function with setInterval in my javascript code. I would like to somehow register a function with my C++ module that gets called repeatedly whenever a new frame is available from the camera. It should behave like setInterval, but instead of being triggered every second, it gets triggered when a frame is available.

The code that I am hoping for on the javascript-side would be something like:

MyCPPModule.setMyFrameInterval(function (msg) {
        console.log(msg);
    });

Inside the C++ Module I would like to do something like:

void NewFrameAvailable(char** imageBuffer, /* more arguments */)
{ 
    // ... 
    frameCounter++;  // increment global variable
    Local<Function> cb = /* WHAT DO I DO HERE*/ ;
    Isolate*isolate = /* WHAT DO I DO HERE */;
    const unsigned argc = 1;
    Local<Value> argv[argc] = { String::NewFromUtf8(isolate, std::to_string(frameCounter)) };
    cb->Call(Null(isolate), argc, argv);
    // ...
}

and a function that registers the javascipt-callback with setMyFrameInterval:

void setMyFrameInterval(const FunctionCallbackInfo<Value>& args) {
    Local<Function> cb = Local<Function>::Cast(args[0]); // make this somehow global? 
    Isolate*isolate = args.GetIsolate()
    //...
} 

So, how can I invoke the javascript callback from NewFrameAvailable (the C++ callback function which gets triggered when frames are available). I think I basically have to make the javascript function somehow globally available in the setMyFrameInterval function so that it is also known to the newFrameAvailable function. How do I do this?

来源:https://stackoverflow.com/questions/33923106/invoke-javascript-callback-repeatedly-from-c-module-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!