用于grpc服务器的节点js客户端

时间:2017-05-18 22:41:11

标签: node.js client chat grpc

我使用openssl运行GRPC服务器 - 静态方式,我尝试使用nodejs客户端连接到服务器

我没有看到任何错误,但我也没有看到它连接到服务器。 如果您有任何样品,请分享。

请参阅以下代码:

    var rootCertPath = path.join('.','.', 'server-root.PEM');
    var privateCertPath = path.join('.','.', 'server-private.PEM');
    var domainCertPath = path.join('.','.', 'server-domain.PEM');

    var rootCert = fs.readFileSync(rootCertPath);
    var privateCert = fs.readFileSync(privateCertPath);
    var domainCert = fs.readFileSync(domainCertPath);

    var buf1 = new Buffer('rootCert');
    var buf2 = new Buffer('privateCert');
    var buf3 = new Buffer('domainCert');

    var chat_proto = grpc.load("Chat.proto").com.company.grpc;
    var client = new chat_proto.ChatService('https://servervip:443',grpc.credentials.createSsl(buf1,buf2,buf3));

Chat.proto

  syntax = "proto3";

        // Service definition.

            service ChatService {
              // Sends a chat
               rpc chat(stream ChatMessage) returns (stream ChatMessageFromServer) {}
            }
        // The request message containing the user's name.
        message ChatMessage {
          string name = 1;
          string message = 2;
        }

        // The response message containing the greetings
        message ChatMessageFromServer {
            string name = 1;
            string message = 2;
        }

//发出请求的代码

var username = process.argv[2];
var stdin = process.openStdin();

function main() {
    console.log("starting");
    console.log(client); // prints { '$channel': Channel {} }
    var chat=client.chat();
    chat.on('data', function(msg) {
      console.log(msg.name + ': ' + msg.message);
        console.log("after message");
    });

    stdin.addListener('data',function(input) {
        chat.write({ name: username, message: input.toString().trim()
        }); 

    });

}
main();

1 个答案:

答案 0 :(得分:2)

这么好的新东西 - 下面的东西对我有用

var rootCertPath = path.join('.','.', 'roots.PEM');
var rootCert = fs.readFileSync(rootCertPath);
var chat_proto = grpc.load("Chat.proto").com.americanexpress.grpc.chat;
var client = new chat_proto.ChatService('servervip:443',grpc.credentials.createSsl(rootCert));

看起来像证书的问题 - 我在grpc客户端使用了默认的roots.PEM,它对我有用。将在内部查找具有我的servervip CA证书链的正确根目录。

感谢大家的支持

相关问题