2017-05-29 154 views
0

沒有錯誤,沒有迴應我的客戶。我message.proto:NodeJs沒有響應gRPC服務器

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); 
} 

我GRPC服務器:

'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(); 

我的客戶:

'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); 
    }); 
} 

當我做了一個 「本地主機捲曲:7777」 命令,服務器顯示的SSL握手錯誤,所以我知道它在聽。客戶端顯示:

secure connection established with gRPC server 
in list 
in snd 

而就是這樣。任何一方都沒有錯誤。我嘗試沒有SSL,並得到完全相同的結果。

我的版本: 節點--version v6.9.5

protoc --version libprotoc 2.6.1

NPM --version 3.10.10

任何幫助極大的讚賞。

TIA

+2

我注意到你說'curl localhost:7777'按預期執行SSL握手,但是你構建你的客戶端來連接到'hyperledger-devenv:7777'。你確定解析爲'localhost'嗎?另外,我應該指出,當客戶端構造函數完成時,並不意味着連接已經建立。第一次發出請求時會嘗試連接。 – murgatroid99

回答

0

murgatroid99你是對的。通過簡單地將計算機名添加到我的/ etc/hosts文件中,我解決了這個問題。

127.0.0.1 hyperledger-devenv的

名字 「hyperledger-devenv的」 我是放置在流浪的配置 「Vagrantfile」 爲虛像名的值。這是顯示在我的SSH提示。真實的計算機名稱可以使用hostname命令顯示。

對於那些有興趣,我已經證明我在博客中的所有步驟的: https://bertrandszoghy.wordpress.com/2017/05/30/send-and-receive-protocol-buffers-securely-with-grpc-in-nodejs/

謝謝了!