【问题标题】:Does Protobuf support Tuples?Protobuf 是否支持元组?
【发布时间】:2021-11-15 21:16:17
【问题描述】:

我想从 grpc 服务器流式传输字符串元组。

我似乎找不到一个聪明的方法(如果有的话)可以做到这一点。你们中的任何人都对 Protobuf 中的元组有过好运吗?

附加信息:

我正在使用 F#,并且想要相应的东西

string * string

【问题讨论】:

  • 注意:(string*string)System.Tuple<string, string>

标签: c# f# protocol-buffers grpc


【解决方案1】:

您使用的是什么库?我使用protobuf-net.Grpc 没有问题。我的服务如下所示:

member __.SubscribeTupleAsync() =
    asyncSeq {
        while true do
            let time = DateTime.Now
            yield string time.Minute, string time.Second
            do! Async.Sleep 1000
    } |> AsyncSeq.toAsyncEnum

我的客户看起来像这样:

use http = GrpcChannel.ForAddress("http://localhost:10042")
let client = http.CreateGrpcService<ITimeService>()
async {
    for (min, sec) in client.SubscribeTupleAsync() |> AsyncSeq.ofAsyncEnum do
        printfn "%s, %s" min sec
} |> Async.RunSynchronously

合同是:

[<ServiceContract>]
type ITimeService =
    abstract member SubscribeTupleAsync : unit -> IAsyncEnumerable<string * string>

.proto 文件是:

syntax = "proto3";
package ProtobufCommon;
import "google/protobuf/empty.proto";

message Tuple_String_String {
   string Item1 = 1;
   string Item2 = 2;
}
service TimeService {
   rpc SubscribeTuple (.google.protobuf.Empty) returns (stream Tuple_String_String);
}

客户端的输出是:

3, 26
3, 27
3, 28
3, 29
3, 30
3, 31
3, 32
3, 33
3, 34
3, 35
...

【讨论】:

  • 好像我做错了,这帮助了很多欢呼!
  • 太棒了。如果您觉得有用,请采纳答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-16
  • 1970-01-01
  • 2015-02-12
  • 1970-01-01
  • 2023-03-21
  • 2011-06-13
  • 1970-01-01
相关资源
最近更新 更多