Skip to content

Commit 3891ab0

Browse files
committed
Documented streaming-rpc example a bit [ci skip]
1 parent 9ea3766 commit 3891ab0

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ node_modules/
55
types/types.d.ts
66
bench.txt
77
docs/
8+
examples/

examples/streaming-rpc.js

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var protobuf = require("..");
22

3+
// Load a definition with services:
4+
35
var root = protobuf.Root.fromJSON({
46
nested: {
57
Greeter: {
@@ -31,35 +33,39 @@ var root = protobuf.Root.fromJSON({
3133
}
3234
});
3335

36+
// Get its types:
37+
3438
var Greeter = root.lookup("Greeter"),
3539
Hello = root.lookup("Hello"),
3640
World = root.lookup("World");
3741

38-
var ended = false;
42+
// Provide a stream-aware RPC implementation:
3943

40-
// Implement: Stream-aware RPC implementation
41-
function rpcImpl(method, requestData, callback) {
42-
if (ended)
43-
return;
44-
if (!requestData) {
45-
console.log("rpc ended client-side.");
46-
ended = true;
47-
return;
48-
}
49-
setTimeout(function() {
50-
try {
51-
// <exemplary server side code>
52-
var hello = Hello.decodeDelimited(requestData);
53-
var responseData = World.encodeDelimited({ message: 'Hello ' + hello.name + ' !' }).finish();
54-
// </exemplary server side code>
55-
callback(null, responseData);
56-
} catch (err) {
57-
callback(err);
44+
var greeter = Greeter.create(/* rpcImpl */ (function() { // API documentation: Service#create
45+
var ended = false;
46+
return function myRPCImpl(method, requestData, callback) {
47+
if (ended)
48+
return;
49+
if (!requestData) {
50+
console.log("rpc ended client-side.");
51+
ended = true;
52+
return;
5853
}
59-
}, Math.random() * 500);
60-
}
54+
setTimeout(function() {
55+
try {
56+
// begin exemplary server side code
57+
var hello = Hello.decodeDelimited(requestData);
58+
var responseData = World.encodeDelimited({ message: 'Hello ' + hello.name + ' !' }).finish();
59+
// end exemplary server side code
60+
callback(null, responseData);
61+
} catch (err) {
62+
callback(err);
63+
}
64+
}, Math.random() * 500);
65+
};
66+
})(), /* requestDelimited? */ true, /* responseDelimited? */ true);
6167

62-
var greeter = Greeter.create(rpcImpl, true, true);
68+
// Listen for events:
6369

6470
greeter.on("data", function(response, method) {
6571
console.log("data:", response.message);
@@ -73,14 +79,25 @@ greeter.on("error", function(err, method) {
7379
console.log("error:", err);
7480
});
7581

82+
// Call methods:
83+
7684
greeter.sayHello({ name: 'protocol' });
7785
greeter.sayHello({ name: 'buffers' });
78-
greeter.sayHello({ name: 'for' });
86+
greeter.sayHello(Hello.create({ name: 'for' })); // or use runtime messages
87+
88+
// Listen to and emit your own events if you want:
89+
90+
greeter.on("status", function(code, text) {
91+
console.log("status:", code, text);
92+
});
93+
greeter.emit("status", 200, "OK");
94+
95+
// And, if applicable, end the service when you are done:
7996

8097
setTimeout(function() {
8198
greeter.end();
8299
// ^ Signals rpcImpl that the service has been ended client-side by calling it with a null buffer.
83-
// Likewise, rpcImpl can end the stream by calling its callback with an explicit null buffer.
100+
// Likewise, rpcImpl can also end the stream by calling its callback with an explicit null buffer.
84101

85102
greeter.sayHello({ name: 'javascript' }); // does nothing
86103
}, 1000);

src/service.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,11 @@ ServicePrototype.remove = function remove(object) {
160160
ServicePrototype.create = function create(rpcImpl, requestDelimited, responseDelimited) {
161161
var rpcService = new rpc.Service(rpcImpl);
162162
this.getMethodsArray().forEach(function(method) {
163-
var lcName = method.name.substring(0, 1).toLowerCase() + method.name.substring(1);
164-
rpcService[lcName] = function(request, callback) {
163+
rpcService[method.name.substring(0, 1).toLowerCase() + method.name.substring(1)] = function callVirtual(request, /* optional */ callback) {
165164
if (!rpcService.$rpc) // already ended?
166165
return;
166+
if (!request)
167+
throw util._TypeError("request", "not null");
167168
method.resolve();
168169
var requestData;
169170
try {

0 commit comments

Comments
 (0)