-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-client.js
55 lines (49 loc) · 1.19 KB
/
node-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var PROTO_PATH = __dirname + '/helloworld.proto';
var async = require('async');
var grpc = require('@grpc/grpc-js');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
PROTO_PATH,
{keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
var protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
var helloworld = protoDescriptor.helloworld;
var client = new helloworld.Greeter('localhost:9090',
grpc.credentials.createInsecure());
/**
* @param {function():?} callback
*/
function runSayHello(callback) {
client.sayHello({name: 'John'}, {}, (err, response) => {
console.log(response.message);
callback();
});
}
/**
* @param {function():?} callback
*/
function runSayRepeatHello(callback) {
var stream = client.sayRepeatHello({name: 'John', count: 5}, {});
stream.on('data', (response) => {
console.log(response.message);
});
stream.on('end', () => {
callback();
});
}
/**
* Run all of the demos in order
*/
function main() {
async.series([
runSayHello,
runSayRepeatHello,
]);
}
if (require.main === module) {
main();
}