-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathclient-bidi-stream.js
46 lines (39 loc) · 1.27 KB
/
client-bidi-stream.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
const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');
const grpc_promise = require('../lib/index'); // require('grpc-promise')
const packageDefinition = protoLoader.loadSync(
__dirname + '/protobuf/test.proto',
{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
}
);
const protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
const test_proto = protoDescriptor.test;
function main() {
const client = new test_proto.Test('localhost:50052', grpc.credentials.createInsecure());
const meta = new grpc.Metadata();
meta.add('key', 'value');
// Optional timeout definition, defaults = 50
grpc_promise.promisifyAll(client, { timeout_message: 100, metadata: meta, timeout: 1000 }); // timeout in milliseconds
let t = client.testStreamStream();
t.sendMessage({})
.then(res => {
console.log('Client: Stream Message Received = ', res); // Client: Stream Message Received = {id: 0}
})
.catch(err => console.error(err))
;
t.sendMessage({})
.then(res => {
console.log('Client: Stream Message Received = ', res); // Client: Stream Message Received = {id: 1}
})
.catch(err => console.error(err))
;
setTimeout(function () {
t.end();
}, 200);
}
main();