问题
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