Example #1
0
        static async Task Main(string[] args)
        {
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

            var channel = GrpcChannel.ForAddress("http://localhost:5000");
            var client  = new Board.BoardClient(channel);

            using var tokenSource = new CancellationTokenSource();
            CancellationToken token = tokenSource.Token;

            using var stream = client.ShowMessage(cancellationToken: token);

            bool response = true;
            int  inc      = 1;

            do
            {
                try
                {
                    var delay = checked (1000 * inc);

                    await stream.RequestStream.WriteAsync(new MessageRequest
                    {
                        Ping      = "Ping",
                        DelayTime = delay
                    });

                    inc++;
                    Console.WriteLine($"Send ping on {DateTimeOffset.UtcNow} \n");

                    response = await stream.ResponseStream.MoveNext(token);

                    if (response)
                    {
                        var result = stream.ResponseStream.Current;
                        Console.WriteLine($"Receive {result.Pong} on {DateTimeOffset.UtcNow} \n\n");
                    }
                }
                catch (OverflowException)
                {
                    inc = 1;
                }
            } while (response);


            Console.WriteLine("End");
            Console.ReadLine();
        }
Example #2
0
        static async Task Main(string[] args)
        {
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            var channel = GrpcChannel.ForAddress("http://localhost:5000");
            var client  = new Board.BoardClient(channel);

            using var tokenSource = new CancellationTokenSource();
            CancellationToken token = tokenSource.Token;

            using var stream = client.ShowMessage(cancellationToken: token);

            foreach (var f in Fortunes)
            {
                if (token.IsCancellationRequested)
                {
                    break;
                }

                await stream.RequestStream.WriteAsync(new MessageRequest
                {
                    FortuneCookie = f
                });

                Console.WriteLine($"Sending \"{f}\" \n");
                await Task.Delay(1000, token);
            }

            await stream.RequestStream.CompleteAsync();

            var response = await stream.ResponseAsync;

            Console.WriteLine($"\n\n");

            foreach (var r in response.Fortunes)
            {
                Console.WriteLine(
                    $"Reply \"{r.Message}\". Original cookied received on {new DateTime(r.ReceivedTime)}. \n");
            }

            Console.WriteLine("End");
            Console.ReadLine();
        }
Example #3
0
        static async Task Main(string[] args)
        {
            //We need this switch because we are connecting to an unsecure server. If the server runs on SSL, there's no need for this switch.
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            var channel = GrpcChannel.ForAddress("http://localhost:5000");
            var client  = new Board.BoardClient(channel);
            var result  = client.ShowMessage(new MessageRequest
            {
                Name = "Johny"
            });

            using var tokenSource = new CancellationTokenSource();
            CancellationToken token = tokenSource.Token;

            var streamReader = result.ResponseStream;

            await foreach (var reply in streamReader.ReadAllAsync(token))
            {
                var displayDate = new DateTime(reply.DisplayTime);
                Console.WriteLine($"Received \"{reply.Message}\" on {displayDate.ToLongTimeString()} \n");
            }

            Console.ReadLine();
        }