-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhwsc-grpc-sample-svc.js
48 lines (38 loc) · 1.29 KB
/
hwsc-grpc-sample-svc.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
const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');
const moment = require('moment');
const PROTO_PATH = `${__dirname}/../..`;
const options = {
includeDirs: [PROTO_PATH],
};
const hwscGrpcSampleSvcProtoPkgDef = protoLoader
.loadSync('protobuf/hwsc-grpc-sample-svc/proto/hwsc-grpc-sample-svc.proto', options);
const hwscGrpcSampleSvcPbJs = grpc.loadPackageDefinition(hwscGrpcSampleSvcProtoPkgDef)
.hwscGrpcSampleSvc;
function sayHello(callback) {
if (typeof callback !== 'function') {
console.error('callback not a function');
return;
}
const client = new hwscGrpcSampleSvcPbJs.SampleService('localhost:50051',
grpc.credentials.createInsecure());
const now = moment().utc();
console.log('Sending request with UTC unix:', now.unix());
console.log('Sending request with UTC date:', now.format());
const request = {
firstName: 'Lisa',
createTimestamp: now.unix(),
};
client.sayHello(request, (err, response) => {
if (!err) {
const dateString = moment.unix(response.responseTimestamp).utc().format();
console.log('Receiving response.msg:', response.message);
console.log('Receiving UTC date:', dateString);
grpc.closeClient(client);
}
callback(err, response);
});
}
module.exports = {
sayHello,
};