mirror of https://github.com/grpc/grpc-node.git
88 lines
3.5 KiB
JavaScript
88 lines
3.5 KiB
JavaScript
var client = require('../surface_client.js');
|
|
|
|
var builder = ProtoBuf.loadProtoFile(__dirname + '/../examples/math.proto');
|
|
var math = builder.build('math');
|
|
|
|
/**
|
|
* Get a function that deserializes a specific type of protobuf.
|
|
* @param {function()} cls The constructor of the message type to deserialize
|
|
* @return {function(Buffer):cls} The deserialization function
|
|
*/
|
|
function deserializeCls(cls) {
|
|
/**
|
|
* Deserialize a buffer to a message object
|
|
* @param {Buffer} arg_buf The buffer to deserialize
|
|
* @return {cls} The resulting object
|
|
*/
|
|
return function deserialize(arg_buf) {
|
|
return cls.decode(arg_buf);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Serialize an object to a buffer
|
|
* @param {*} arg The object to serialize
|
|
* @return {Buffer} The serialized object
|
|
*/
|
|
function serialize(arg) {
|
|
return new Buffer(arg.encode.toBuffer());
|
|
}
|
|
|
|
/**
|
|
* Sends a Div request on the channel.
|
|
* @param {client.Channel} channel The channel on which to make the request
|
|
* @param {*} argument The argument to the call. Should be serializable with
|
|
* serialize
|
|
* @param {function(?Error, value=)} The callback to for when the response is
|
|
* received
|
|
* @param {array=} Array of metadata key/value pairs to add to the call
|
|
* @param {(number|Date)=} deadline The deadline for processing this request.
|
|
* Defaults to infinite future
|
|
* @return {EventEmitter} An event emitter for stream related events
|
|
*/
|
|
var div = client.makeUnaryRequestFunction('/Math/Div',
|
|
serialize,
|
|
deserialize(math.DivReply));
|
|
|
|
/**
|
|
* Sends a Fib request on the channel.
|
|
* @param {client.Channel} channel The channel on which to make the request
|
|
* @param {*} argument The argument to the call. Should be serializable with
|
|
* serialize
|
|
* @param {array=} Array of metadata key/value pairs to add to the call
|
|
* @param {(number|Date)=} deadline The deadline for processing this request.
|
|
* Defaults to infinite future
|
|
* @return {EventEmitter} An event emitter for stream related events
|
|
*/
|
|
var fib = client.makeServerStreamRequestFunction('/Math/Fib',
|
|
serialize,
|
|
deserialize(math.Num));
|
|
|
|
/**
|
|
* Sends a Sum request on the channel.
|
|
* @param {client.Channel} channel The channel on which to make the request
|
|
* @param {function(?Error, value=)} The callback to for when the response is
|
|
* received
|
|
* @param {array=} Array of metadata key/value pairs to add to the call
|
|
* @param {(number|Date)=} deadline The deadline for processing this request.
|
|
* Defaults to infinite future
|
|
* @return {EventEmitter} An event emitter for stream related events
|
|
*/
|
|
var sum = client.makeClientStreamRequestFunction('/Math/Sum',
|
|
serialize,
|
|
deserialize(math.Num));
|
|
|
|
/**
|
|
* Sends a DivMany request on the channel.
|
|
* @param {client.Channel} channel The channel on which to make the request
|
|
* @param {array=} Array of metadata key/value pairs to add to the call
|
|
* @param {(number|Date)=} deadline The deadline for processing this request.
|
|
* Defaults to infinite future
|
|
* @return {EventEmitter} An event emitter for stream related events
|
|
*/
|
|
var divMany = client.makeBidiStreamRequestFunction('/Math/DivMany',
|
|
serialize,
|
|
deserialize(math.DivReply));
|
|
|
|
var channel = new client.Channel('localhost:7070');
|