c# - 如何配置 GRPC 双向流以在 .NET 中发送字节
问题描述
我正在尝试通过我的双向流发送几千字节的消息。我正在测试流来回发送数据的速度,但是当我尝试写入时,我似乎无法获得要显示的实际值。理想情况下,我希望流尽可能快地来回发送 KB,但现在我无法让它显示。当我尝试运行时,我得到了 Google.Protobuf.ByteString。
这是原型:
service Racer {
rpc ReadySetGo (stream RaceMessageRequest) returns (stream RaceMessageReply);
}
message RaceMessageRequest {
int32 countRequest = 1;
bytes byteRequest = 2;
}
message RaceMessageReply{
int32 countReply = 1;
bytes byteReply = 2;
}
和服务:
public override async Task ReadySetGo(
IAsyncStreamReader<RaceMessageRequest> requestStream,
IServerStreamWriter<RaceMessageReply> responseStream,
ServerCallContext context)
{
////////////////////////////////////
//Creating a stream
await foreach (var request in requestStream.ReadAllAsync())
{
var helloReply = new RaceMessageReply { CountReply = 25 + request.CountRequest };
await responseStream.WriteAsync(helloReply);
}
//testing the binary data streaming
await foreach (var request in requestStream.ReadAllAsync())
{
var hellobytes = new RaceMessageReply { ByteReply = ByteString.CopyFrom((byte)request.CountRequest) };
await responseStream.WriteAsync(hellobytes);
}
///////////////////////////////
我在主要方法中的代码:
byte[] bytes = BitConverter.GetBytes(10000);
using var call = client.ReadySetGo();
while (true)
{
// Send and receive messages over the stream
await call.RequestStream.WriteAsync(new RaceMessageRequest { CountRequest = 24 });
await call.RequestStream.WriteAsync(new RaceMessageRequest { ByteRequest = ByteString.CopyFrom(bytes) });
await call.ResponseStream.MoveNext();
//Console.WriteLine($"Greetings testing the basic format: {call.ResponseStream.Current.CountReply}");
Console.WriteLine($"Greetings testing Byte stream: {call.ResponseStream.Current.ByteReply.ToString()}");
Console.ReadKey();
}
解决方案
推荐阅读
- sql - 我应该使用特定的 SQL 语法为 SAP 中的 ABAP 的 AutoID 编写 SQL 吗?
- c++ - Q_PROPERTY 未将值设置为通过 qml 上的模型给出的值
- arrays - 用于匹配特定字符的正则表达式
- python - 由于“TypeError: A Future or coroutine is required”,无法在 AWS Lambda 上发出 aiohttp 请求
- python - 减去 DataFrame 列中的元素
- javascript - 如何限制此脚本适用的列?
- datastage - DataStage 将数据从顺序文件插入到 ODBC 阶段
- r - R Case语句同时使用and或在同一语句中-在日期之间,有些在多列中缺少值
- python - 在 Python 3.7 cli 中运行命令
- python - 如何使这个查询从 Mongo 4.0 到 Mongo 3.6