且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Grpc-从一个客户端向连接到同一服务器的另一个客户端发送消息

更新时间:2022-06-21 21:30:26

没有特殊的gRPC功能可以执行此操作(所有RPC都在服务器和客户端之间,没有广播"或选择性广播"功能可以连接到连接到同一服务器的其他客户端.

There's no special gRPC feature that would allow you to do this (all RPC are between a server and a client, there's no "broadcast" or "selective broadcast" feature to reach out to other clients connected to the same server.

您想要的逻辑肯定可以实现,但是这种解决方案的细节取决于您的需要.可能有效的幼稚方法是这个:

The logic you want is something that can definitely be implemented and but details of such solution depend on your need. A naive approach that might work is e.g. this:

  • 每个客户端向服务器打开一个双向出价调用
  • 服务器保留已连接客户端的目录
  • 一旦服务器接收到来自客户端的消息,它就会根据目录选择应转发给哪个客户端.
  • 服务器将邮件转发给客户端.

不用说,此设置感觉有点复杂(您基本上是在gRPC之上实现自己的网络协议),因此即使这可能是您正确的选择,但考虑一下还是有道理的如何简化协议,以便可以使用gRPC直接支持的功能.

Needless to say this setup feels a bit complicated (you're basically implementing your own network protocol on top of gRPC), so even though it might be just the right thing for you to do, it would make sense to think about how to simplify the protocol so that you can use features directly supported by gRPC.