如何使用grpc通过服务器在客户端之间发送消息? [英] how to send messages between clients through server using grpc?

查看:1183
本文介绍了如何使用grpc通过服务器在客户端之间发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个程序,以便客户端能够相互发送消息。因此,当客户端发送消息时,该消息将发送到服务器,该服务器将其发送到接收方客户端。问题在于,如果没有该客户端先发送消息,服务器就无法将消息发送给客户端,因此,如果客户端未先发送任何消息,那么客户端如何通过服务器简单地从另一个客户端接收消息。

i am trying to create a program so clients are able to send messages to each other. So when a client sends a message, that message is sent to the server which sends it on to the recipient client. the problem im having is that there is no way for the server to send messages to a client without that client sending a message first so how can a client simply receive a message from another client through the server if its not sending anything first.

这是我的原始文件:

service Messenger {
    rpc SendMessage (stream Message) returns (stream Message) {}
}

message Message {
  string msg = 1;
  receiverId = 2;
}

我还生成了类文件,并编写了用于发送的客户端和服务器函数此示例之后的消息: https:/ /github.com/grpc/grpc/blob/v1.15.0/examples/csharp/RouteGuide/RouteGuideServer/RouteGuideImpl.cs 。目前,它仅允许客户端将消息发送到服务器。我现在正在寻找一种方法来

I have also generated the class files and written the client and server functions for sending messages following this example: https://github.com/grpc/grpc/blob/v1.15.0/examples/csharp/RouteGuide/RouteGuideServer/RouteGuideImpl.cs. at the moment, it only lets the client send messages to the server. I am now looking for a way to either

1)从服务器向客户端发送消息,该消息具有与消息中指定的receiveId相同的ID

1) send message from server to the client that has the same id as the receiverId specified in the message

OR

2)香港专业教育学院听说你可以让客户以某种方式订阅来自某些客户的消息,但我找不到任何方法

2) ive heard u can have clients subscribe to messages somehow from certain clients but i cant find anything on how to do it

感谢您的帮助!谢谢

推荐答案

所有RPC都是由客户端启动的。

> https://grpc.io/docs/guides/concepts.html

All RPC are started by the client.
https://grpc.io/docs/guides/concepts.html

从服务器向客户端发送消息意味着客户端接收到一条消息。发送和接收(订阅)是不同的操作。信使服务可以是这样的:

Sending a message from server to client means the client receives a message. Sending and receiving (subscribing) are different operation. The messenger service can be like this:

service Messenger {
  rpc SendMessage (Message) returns (google.protobuf.Empty) {}
  rpc SubscribeMessages (google.protobuf.Empty) returns (stream Message) {}
}

仅供参考,我使用gRPC作为示例实现制作了聊天服务器-客户端应用程序。
https://github.com/cactuaroid/GrpcWpfSample

FYI, I have made chat server-client application using gRPC as a sample implementation. https://github.com/cactuaroid/GrpcWpfSample

如果要标识客户端,则可以指定 SubscribeMessages(id)之类的id,以便服务器可以过滤返回的消息等,这不是gRPC的问题。

If you want to identify the clients, you can specify id like SubscribeMessages(id) so that the server can filter the returning messages etc., it is not a problem of gRPC.

这篇关于如何使用grpc通过服务器在客户端之间发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆