【问题标题】:Calling callback inside callback in native Node在本机节点的回调中调用回调
【发布时间】:2017-10-24 15:38:40
【问题描述】:

我是节点和 C++ 新手,所以请宽容。

我正在编写一个本地节点插件。

我的插件启动网络摄像头流式传输(使用 UVC 库),我希望每个帧都可用于节点。

我的 CC 插件会做类似的事情

uvc_start_streaming(devh, &ctrl, frameProcess, (void *) &args, 0)

地点:

  • devh:是UVC设备
  • ctrl: 是设备配置
  • frameProcess: 是我在每个新帧调用的函数回调
  • args:是来自 javascript 函数的参数。

每个新帧都会调用 c++ 回调,我想简单地打印“收到新帧”之类的内容,所以我的 C++ 是这样的:

void frameProcess(uvc_frame_t *frame, void *ptr) {
  const FunctionCallbackInfo<Value> args = *((const FunctionCallbackInfo<Value>*)(ptr));
  Isolate* isolate = args.GetIsolate();

  Local<Function> cb = Local<Function>::Cast(args[0]);  
  const unsigned argc = 1;
  Local<Value> argv[argc] = { String::NewFromUtf8(isolate, "new frame received") };
  cb->Call(Null(isolate), argc, argv);

}

void testStreaming (const FunctionCallbackInfo<Value>& args) {
    ...
    res = uvc_start_streaming(devh, &ctrl, frameProcess, (void *) &args, 0);
    puts("Streaming...");
    Sleep(10000); /* stream for 10 seconds */
    uvc_stop_streaming(devh);
    puts("Done streaming.");
    ...
}

...
NODE_SET_METHOD(exports, "testStreaming", testDevice);

我的 js 是这样的:

'use strict';
var uvc = require('../build/Release/binding')
uvc.testStreaming(
  function (x) {
    console.log(x)
  }
)

问题是当程序到达 cb->Call 时,节点退出时没有任何消息或错误。

如果我评论 cb->Call row 程序按照程序运行 10 秒(持续调用 )然后退出。

但是如果我取消注释 cb->立即调用程序退出。

【问题讨论】:

    标签: javascript c++ node.js callback v8


    【解决方案1】:

    您的 frameProcess() 函数应该在 Node.js 线程中调用 v8::Function 回调,请参阅 https://stackoverflow.com/a/28116160/1355844

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-14
      • 1970-01-01
      • 1970-01-01
      • 2017-01-21
      • 2015-10-13
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      相关资源
      最近更新 更多