I am trying to implement server-side streaming rpc to send messages from server to client.
rpc CheckSystem(CheckSystemRequest) returns (stream CheckSystemResponse) {}
Server code:
public override Task CheckSystem(CheckSystemRequest request, IServerStreamWriter<CheckSystemResponse> responseStream, ServerCallContext context) {
return Task.Run(async () => {
for(int i = 1; i <= 10; i++) {
await responseStream.WriteAsync(new CheckSystemResponse { Type = i.ToString() });
Console.WriteLine($"{DateTime.Now}: Sent {i}");
Thread.Sleep(10000);
}
});
}
Client code:
public static async Task TestAsync() {
try {
using (var call = client.CheckSystem(new CheckSystemRequest { Code = "1" })) {
var responseStream = call.ResponseStream;
while (await responseStream.MoveNext()) {
var current = responseStream.Current;
Console.WriteLine($"{DateTime.Now}: Received: Type = {current.Type}");
}
}
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
After starting, I see that on the server side a message is sent every 10 seconds. Screenshot #1.
13.12.2019 **16:31:31**: Sent 1
13.12.2019 **16:31:41**: Sent 2
13.12.2019 16:31:51: Sent 3
13.12.2019 16:32:01: Sent 4
13.12.2019 16:32:11: Sent 5
13.12.2019 16:32:21: Sent 6
13.12.2019 16:32:31: Sent 7
13.12.2019 16:32:41: Sent 8
13.12.2019 16:32:51: Sent 9
13.12.2019 16:33:01: Sent 10
But, the client begins to process the first message only after the server sends the second. Screenshot #2
13.12.2019 **16:31:41**: Received: Type = 1
13.12.2019 **16:31:41**: Received: Type = 2
13.12.2019 16:31:51: Received: Type = 3
13.12.2019 16:32:01: Received: Type = 4
13.12.2019 16:32:11: Received: Type = 5
13.12.2019 16:32:21: Received: Type = 6
13.12.2019 16:32:31: Received: Type = 7
13.12.2019 16:32:41: Received: Type = 8
13.12.2019 16:32:51: Received: Type = 9
13.12.2019 16:33:01: Received: Type = 10
What could be the reason for this behavior? And how can this be fixed?