how to send messages between clients through server using grpc?

狂风中的少年 提交于 2020-06-27 17:38:06

问题


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.

this is my proto file:

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

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

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) send message from server to the client that has the same id as the receiverId specified in the message

OR

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

any help is appreciated! thanks


回答1:


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) {}
}

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

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.



来源:https://stackoverflow.com/questions/52938306/how-to-send-messages-between-clients-through-server-using-grpc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!