dszn2485 2018-03-12 08:01
浏览 73
已采纳

gRPC专用频道

Is it possible to only stream to certain clients from a gRPC server?

I believe what I'm looking for is something like Pusher, where you have a channel for a client and you can publish messages that can be seen only by a client that has access to that channel.

What I'm struggling with is understanding what are the steps we need to take to do something like this.

Thinking about web-sockets I believe we can store each client connection, then we can find that connection and send messages. How can we do a similar thing with gRPC?

  • 写回答

1条回答 默认 最新

  • dpvv37755 2018-03-15 17:54
    关注

    As per as i understood the question. You want to send the the message to the particular client in gRPC. This is very much possible using Server side streaming or Bi-directional streaming in gRPC.

    For example:

    Define a server side streaming or bidi streaming api

    rpc ListFeatures(Rectangle) returns (stream Feature) {}
    

    On Server side:

    func ListFeatures(rect *pb.Rectangle, stream pb.RouteGuide_ListFeaturesServer) error {
      // Save this stream instance in the server on a map or other suitable data structure
      // so that you can query for this stream instance later
      // This will act same like your websocket session
    }
    

    When you want to send something to a specific client then get the stream instance and do

    err := stream.Send(feature); // Any times as required
    

    On the client, it will be waiting for messages like this

    stream, err := client.ListFeatures(ctx, rect)
    
    for {
        feature, err := stream.Recv()
        ...
        // handle message here
    }
    

    Same thing can be done for bidi streaming rpc also. I hope this answers your question

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器