Reputation: 910
No errors, no response to my client. My message.proto:
syntax = "proto3";
message TstCoordinates {
required int32 id = 1;
required string firstname = 2;
required string lastname = 3;
required string email = 4;
required string areacode = 5;
required string phone = 6;
required string extension = 7;
}
message TstId {
required int32 id = 1;
}
message Empty {}
service TstService{
rpc SendCoordinates (TstId) returns (TstCoordinates);
rpc List (Empty) returns (TstCoordinates);
}
My gRPC server:
'use strict';
const fs = require('fs');
const grpc = require('grpc');
const serviceDef = grpc.load("message.proto");
const PORT = 7777;
const cacert = fs.readFileSync('certs/ca.crt'),
cert = fs.readFileSync('certs/server.crt'),
key = fs.readFileSync('certs/server.key'),
kvpair = {
'private_key': key,
'cert_chain': cert
};
const creds = grpc.ServerCredentials.createSsl(cacert, [kvpair]);
var tstcoordinates = [
{
id: 1,
firstname: "Bill",
lastname: "Williams",
email: "[email protected]",
areacode: "444",
phone: "555-1212",
extension: "378"
},
{
id: 2,
firstname: "Happy",
lastname: "Golucky",
email: "[email protected]",
areacode: "444",
phone: "555-1212",
extension: "382"
}
];
var server = new grpc.Server();
server.addService(serviceDef.TstService.service, {
list: function(call, callback) {
console.log("in list");
callback(null, tstcoordinates[0]);
},
sendCoordinates: function(call, callback) {
console.log("in sendCoordinates");
callback(null, tstcoordinates[1] );
return;
}
});
server.bind(`0.0.0.0:${PORT}`, creds);
console.log(`Starting gRPC server on port ${PORT}`);
server.start();
My client:
'use strict';
const fs = require('fs');
const process = require('process');
const grpc = require('grpc');
const serviceDef = grpc.load("message.proto");
const PORT = 7777;
const cacert = fs.readFileSync('certs/ca.crt'),
cert = fs.readFileSync('certs/client.crt'),
key = fs.readFileSync('certs/client.key'),
kvpair = {
'private_key': key,
'cert_chain': cert
};
const creds = grpc.credentials.createSsl(cacert, key, cert);
const client = new serviceDef.TstService(`hyperledger-devenv:${PORT}`,creds);
console.log("secure connection established with gRPC server");
lst();
snd();
function printResponse(error, response) {
console.log("in printResponse");
if (error)
console.log('Error: ', error);
else
console.log(response);
}
function lst() {
console.log("in list");
client.list({}, function(error, response) {
console.log("in list call");
printResponse(error, response);
});
}
function snd() {
console.log("in snd");
client.sendCoordinates({'id': 1}, function(error, response) {
console.log("in snd call");
printResponse(error, response);
});
}
When I do a "curl localhost:7777" command, the server displays an SSL handshake error so I know it's listening. The client displays:
secure connection established with gRPC server
in list
in snd
And that's all. No errors on either side. I tried without SSL and get exactly the same result.
My versions: node --version v6.9.5
protoc --version libprotoc 2.6.1
npm --version 3.10.10
Any help greatly appreciated.
TIA
Upvotes: 1
Views: 1662
Reputation: 910
murgatroid99 you were right. By simply adding the computername to my /etc/hosts file, I resolved the issue.
127.0.0.1 hyperledger-devenv
The name "hyperledger-devenv" is the value I placed in Vagrant's configuration "Vagrantfile" for the virtual image name. This was showing up in my SSH prompt. The real computer name can be displayed with the hostname command.
For those interested, I have documented all my steps in a blog post at: https://bertrandszoghy.wordpress.com/2017/05/30/send-and-receive-protocol-buffers-securely-with-grpc-in-nodejs/
Thanks much!
Upvotes: 1