如何在grpc双向rpc中发送消息,而不需要grpc节点客户端的缓冲

RouteChat是一个双向rpc的例子 ,似乎是在写入时缓冲消息。 有没有办法强制写入输出stream立即不缓冲?

您看到这种缓冲行为的原因是因为写入实际上是asynchronous完成的,因此服务器代码需要定期在写入之间进行其他asynchronous操作,以确保在计算后尽快发送它们。 您可以利用可选的callback参数进行write以在发送最后一个消息之后立即发送每条消息。 该示例中的代码可以改为使用async.each编写:

 async.each(notes, function(note, next) { console.log('Sending message "' + note.message + '" at ' + note.location.latitude + ', ' + note.location.longitude); var noteMsg = new messages.RouteNote(); noteMsg.setMessage(note.message); var location = new messages.Point(); location.setLatitude(note.location.latitude); location.setLongitude(note.location.longitude); noteMsg.setLocation(location); // Note the use of the next callback here call.write(noteMsg, next); }, function() { // End the stream after all messages have been sent call.end(); });