【发布时间】:2021-09-28 06:26:02
【问题描述】:
我最近刚刚开始尝试使用 gRCP,目前正在尝试使用 node-js 创建一个简单的示例来看看它是如何工作的。但是,在创建要在客户端和服务器之间使用的协议之后。我的 rpc 方法之一没有被调用。
我在.proto文件中的消息规范下方添加了未调用的特定方法。我还在下面提供了客户端调用和服务器端处理程序的示例。
.proto
service WeatherService{
rpc RetrieveAllWeather (void) returns (Temp);
}
message void{}
message Temp{
string msg =1;
}
server.js
// .... server configuration
server.addService(weather_proto.WeatherService.service,
{
"RetrieveAllWeather":RetrieveAllWeather,
"RetrieveWeatherByQuery":RetrieveWeatherByQuery,
"SaveWeatherToFile":SaveWeatherToFile,
}
);
server.bindAsync(
"127.0.0.1:4000",
grpc.ServerCredentials.createInsecure(),
(err,port)=>{
if(err){
console.error(err);
process.exit(1);
}
console.log("Listening on port "+port)
server.start();
}
);
function RetrieveAllWeather(call,callback){
console.log(call);
console.log("working");
callback(null,"call");
}
client.js
const client = new weather_proto.WeatherService(
"127.0.0.1:4000",grpc.credentials.createInsecure()
);
client.RetrieveAllWeather({},(err,res)=>{
if(err){
return err;
}
console.log("working");
console.log(JSON.stringify(res));
});
似乎是因为我试图指定 RetrieveAllWeather 不接受任何内容作为有效负载并发送 json {msg:"call"} 但它不起作用,而我创建的其他方法正在运行。
【问题讨论】:
标签: javascript node.js grpc grpc-node