-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.js
29 lines (24 loc) · 955 Bytes
/
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
const { HelloRequest, RepeatHelloRequest, HelloReply } = require("./proto/helloworld_pb.js");
const { GreeterClient } = require("./proto/helloworld_grpc_pb.js");
var client = new GreeterClient("http://" + window.location.hostname + ":8080", null, null);
// simple unary call
var request = new HelloRequest();
request.setName("World");
client.sayHello(request, {}, (err, response) => {
if (err) {
console.log(`Unexpected error for sayHello: code = ${err.code}` + `, message = "${err.message}"`);
} else {
console.log(response.getMessage());
}
});
// server streaming call
var streamRequest = new RepeatHelloRequest();
streamRequest.setName("World");
streamRequest.setCount(5);
var stream = client.sayRepeatHello(streamRequest, {});
stream.on("data", (response) => {
console.log(response.getMessage());
});
stream.on("error", (err) => {
console.log(`Unexpected stream error: code = ${err.code}` + `, message = "${err.message}"`);
});