Convert to synchronous back hell style required by API
I am trying to integrate kurento with Meteor. I am running into problems in converting nested Node.JS callbacks to correct 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 to meteor server using wrapAsync as shown 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 try to do something like this, I get the function as a result, not an object !. Below is my last commit statement to see sdpanswer.
Exception when calling method 'onOffer' TypeError: Cannot call method 'apply' of undefined
I20150722-19: 10: 15.185 (5.5)? on packages / meteor / helpers.js: 118: 1
I20150722-19: 10: 15.186 (5.5)? in [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)? in packages / ddp / livedata _server.js: 648: 1
I20150722-19: 10: 15.186 (5.5)? in [object Object] ._. extend.withValue (packages / meteor / dynamic_nodejs.js: 56: 1)
I20150722-19: 10: 15,186 (5.5)? in packages / ddp / livedata _server.js: 647: 1
I20150722-19: 10: 15.186 (5.5)? in [object Object] ._. extend.withValue (packages / meteor / dynamic_nodejs.js: 56: 1)
I20150722-19: 10: 15.186 (5.5)? in [object Object] ._. extend.protocol_handlers.method (packages / ddp / livedata_server.js: 646: 1)
I20150722-19: 10: 15,186 (5.5)? in packages / ddp / livedata _server.js: 546: 1
How do I properly convert a callback addon to a good synchronous function using Meteor.wrapAsync
?
source to share
Each callback requires 2 lines, which you are trying to declare and call at the same time. ( http://docs.meteor.com/#/full/meteor_wrapasync )
The first line is to create a synchronized call. The second line is the call to this synchronized call.
clientSync = Meteor.wrapAsync(client.create, client);
pipeline = clientSync('MediaPipeline');
Note that I haven't used kurento, so I can't go into details, but for a general layout, wrapAsync is what you want.
source to share