3

I am trying to integrate kurento with Meteor. I am facing problems in converting the nested callbacks of Node.JS into proper Meteor server code.
Below is my code which I am trying to convert using Meteor.wrapAsync :

kurento(args.ws_uri, function(error, client) {
  if ( error ) return onError(error);

  client.create('MediaPipeline', function(error, pipeline) {
    if ( error ) return onError(error);

    console.log("Got MediaPipeline");

    pipeline.create('RecorderEndpoint', { uri : file_uri }, function(error, recorder) {
      if ( error ) return onError(error);

      console.log("Got RecorderEndpoint");

      pipeline.create('WebRtcEndpoint', function(error, webRtc) {
        if ( error ) return onError(error);

        console.log("Got WebRtcEndpoint");

        webRtc.connect(recorder, function(error) {
          if ( error ) return onError(error);

          console.log("Connected");

          recorder.record(function(error) {
            if ( error ) return onError(error);

            console.log("record");

            webRtc.connect(webRtc, function(error) {
              if ( error ) return onError(error);

              console.log("Second connect");
            });

            webRtc.processOffer(offer, function(error, answer) {
              if ( error ) return onError(error);

              console.log("offer");

              return answer;
            });
          });
        });
      });
    });
  });
});

I am trying to write it in meteor server using wrapAsync as below.

client = Meteor.wrapAsync(kurento,ws_uri);
//client = Meteor.wrapAsync(getKurentoClient);
console.log("got connected to server");

pipeline = Meteor.wrapAsync(client.create,'MediaPipeline');
console.log("Got MediaPipeline");

var webRtc = Meteor.wrapAsync(pipeline.create,'WebRtcEndpoint');
console.log("Got WebRtcEndpoint");

var recorder = Meteor.wrapAsync(pipeline.create,('RecorderEndpoint', {uri: file_uri}));
console.log("Got RecorderEndpoint");

Meteor.wrapAsync(webRtc.connect,recorder);
console.log("Connected recorder");

Meteor.wrapAsync(webRtc.connect,webRtc);
console.log("Connected webRtc");

Meteor.wrapAsync(recorder.record);
console.log("started recording");

var sdpAnswer = Meteor.wrapAsync(webRtc.processOffer,offer);
console.log("sdpAnswer"+sdpAnswer());
return sdpAnswer;

Whenever I am trying to do something like this, I am getting a function as output, not the object!. Below is the output of my last concole log statement to see sdpanswer.

Exception while invoking method 'onOffer' TypeError: Cannot call method 'apply' of undefined
I20150722-19:10:15.185(5.5)? at packages/meteor/helpers.js:118:1
I20150722-19:10:15.186(5.5)? at [object Object].Meteor.methods.onOffer (app/absimpl.js:90:31)
I20150722-19:10:15.186(5.5)? at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1617:1)
I20150722-19:10:15.186(5.5)? at packages/ddp/livedata_server.js:648:1
I20150722-19:10:15.186(5.5)? at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20150722-19:10:15.186(5.5)? at packages/ddp/livedata_server.js:647:1
I20150722-19:10:15.186(5.5)? at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20150722-19:10:15.186(5.5)? at [object Object]._.extend.protocol_handlers.method (packages/ddp/livedata_server.js:646:1)
I20150722-19:10:15.186(5.5)? at packages/ddp/livedata_server.js:546:1

How can I properly convert a callback hell to a nice synchronous function using Meteor.wrapAsync?

4

1 回答 1

3

每个回调需要 2 行,您尝试同时声明和调用。(http://docs.meteor.com/#/full/meteor_wrapasync

第一行是创建一个同步呼叫。第二行是调用该同步调用。

clientSync = Meteor.wrapAsync(client.create, client);
pipeline = clientSync('MediaPipeline');

请注意,我没有使用过 kurento,因此无法详细说明,但对于一般的 wrapAsync 布局,这就是您想要的。

于 2015-07-22T14:26:16.387 回答