Protobuf支持元组吗?

5
我希望能够从grpc服务器流式传输一个字符串元组。 我似乎找不到巧妙的方法(如果有的话),可以实现这个功能。你们中有人在protobuf中使用元组取得了成功吗? 其他信息: 我正在使用F#,并且希望找到相应的方法。
string * string

注意:(string*string) 表示 System.Tuple<string, string> - JL0PD
1个回答

5
你在使用哪个库?我使用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
...

似乎我一直做错了,这个帮了很多忙,谢谢! - tobiasholmdk
很好。如果您觉得有用,请接受答案。 - Brian Berns

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接