转换为同步样式 API 所需的回调地狱
Converting to synchronous-style a callback hell required by an API
我正在尝试将 kurento 与 Meteor 集成。我在将 Node.JS 的嵌套回调转换为正确的 Meteor 服务器代码时遇到问题。
下面是我尝试使用 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;
});
});
});
});
});
});
});
我正在尝试使用 wrapAsync 将其写入流星服务器,如下所示。
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;
每当我尝试做这样的事情时,我得到的是一个函数作为输出,而不是对象!。下面是我上次看sdpanswer的concole日志语句的输出。
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
如何使用 Meteor.wrapAsync
将回调地狱正确转换为良好的同步函数?
每个回调需要 2 行,您正在尝试同时声明和调用。 (http://docs.meteor.com/#/full/meteor_wrapasync)
第一行是创建同步调用。第二行是调用同步调用。
clientSync = Meteor.wrapAsync(client.create, client);
pipeline = clientSync('MediaPipeline');
请注意,我没有使用过 kurento,所以无法详细说明,但对于一般的 wrapAsync 布局,这就是您想要的。
我正在尝试将 kurento 与 Meteor 集成。我在将 Node.JS 的嵌套回调转换为正确的 Meteor 服务器代码时遇到问题。
下面是我尝试使用 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;
});
});
});
});
});
});
});
我正在尝试使用 wrapAsync 将其写入流星服务器,如下所示。
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;
每当我尝试做这样的事情时,我得到的是一个函数作为输出,而不是对象!。下面是我上次看sdpanswer的concole日志语句的输出。
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
如何使用 Meteor.wrapAsync
将回调地狱正确转换为良好的同步函数?
每个回调需要 2 行,您正在尝试同时声明和调用。 (http://docs.meteor.com/#/full/meteor_wrapasync)
第一行是创建同步调用。第二行是调用同步调用。
clientSync = Meteor.wrapAsync(client.create, client);
pipeline = clientSync('MediaPipeline');
请注意,我没有使用过 kurento,所以无法详细说明,但对于一般的 wrapAsync 布局,这就是您想要的。