Beispiel #1
0
        static async Task Main(string[] args)
        {
            var userName = args?.FirstOrDefault() ?? "Me";

            var channel = new Channel("localhost:5001", ChannelCredentials.Insecure);

            var client = new ChatService.ChatServiceClient(channel);

            using (var call = client.Chat())
            {
                var cancellationSignal = new CancellationTokenSource();

                var cancellationTask = Task.Run(() =>
                {
                    Console.WriteLine("Press any key to stop");
                    Console.ReadKey();

                    cancellationSignal.Cancel();
                });

                await call.RequestStream.WriteAsync(new ChatRequest
                {
                    Request = new ChatMessage
                    {
                        From    = userName,
                        Message = $"Hi there, it's {userName}!"
                    }
                });

                Console.WriteLine("Message sent!");

                while (await call.ResponseStream.MoveNext(CancellationToken.None))
                {
                    var message = call.ResponseStream.Current;
                    Console.WriteLine($"Received message from {message.Response.From}: {message.Response.Message}");
                }

                await cancellationTask;
                await call.RequestStream.CompleteAsync();
            }

            await channel.ShutdownAsync();
        }
Beispiel #2
0
        static async Task Main(string[] args)
        {
            var serverAddress = args.Length > 1 ? args[1] : "https://localhost:5001";

            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                // The following statement allows you to call insecure services. To be used only in development environments.
                AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            }

            var channel = GrpcChannel.ForAddress(serverAddress);
            var client  = new Greeter.GreeterClient(channel);

            var greetRequest = new HelloRequest {
                Name = "Uno"
            };

            var reply = await client.SayHelloAsync(greetRequest, deadline : DateTime.UtcNow.AddSeconds(30));

            Console.WriteLine($"Reply: {reply.Message}...");
            Console.ReadKey();

            Console.WriteLine("Client Streaming now....");

            var clientStreamClient = new ClientEvent.ClientEventClient(channel);
            var sent = 0;

            using (var streamCall = clientStreamClient.UploadEvents())
            {
                while (sent < 20)
                {
                    streamCall.RequestStream.WriteAsync(new StringValue {
                        Value = "Some message here"
                    });
                    sent++;
                    await Task.Delay(TimeSpan.FromSeconds(2));
                }
                await streamCall.RequestStream.CompleteAsync();

                var response = await streamCall.ResponseAsync;

                Console.WriteLine($"Call took {response.ElapsedTime} seconds to complete");
                Console.WriteLine($"Call sttaus was {response.CollationStatus}");
            }


            Console.ReadKey();

            Console.WriteLine("Streaming responses now...");

            var pulseClient = new Heartbeat.HeartbeatClient(channel);

            CancellationTokenSource streamCTS = new CancellationTokenSource(TimeSpan.FromSeconds(20));

            while (!streamCTS.IsCancellationRequested)
            {
                AsyncServerStreamingCall <Pulse> response = pulseClient.GetHeartbeatStream(new Empty(), cancellationToken: streamCTS.Token);

                try
                {
                    await foreach (Pulse pulse in response.ResponseStream.ReadAllAsync(cancellationToken: streamCTS.Token))
                    {
                        Console.WriteLine($"[+] {pulse.EventTime} {pulse.Path} {pulse.EventStatus}");
                    }
                }
                catch (RpcException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            Console.ReadKey();

            Console.WriteLine("Bidirectional streaming now...");

            var chatClient = new ChatService.ChatServiceClient(channel);

            using (var call = chatClient.Chat())
            {
                var responseReaderTask = Task.Run(async() =>
                {
                    while (await call.ResponseStream.MoveNext())
                    {
                        var note = call.ResponseStream.Current;
                        Console.WriteLine("Received " + note);
                    }
                });

                string[] dataDump = new string[] {
                    "Sergio Reguilon",
                    "Mendy",
                    "Mason Mount",
                    "Gabriel Heinze",
                    "Gonzalo Higuain"
                };

                foreach (var payload in dataDump)
                {
                    await call.RequestStream.WriteAsync(new StringValue { Value = payload });
                }
                await call.RequestStream.CompleteAsync();

                await responseReaderTask;
            }
        }