Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        private async Task SearchPlayerServerStreamAsync()
        {
            PlayerSearchRequest request = CreatePlayerSearchRequest();

            CallOptions callOptions = CreateCallOptions();

            if (this.UseHttpGateway)
            {
                IList <PlayerSearchResponse> responses = SearchPlayerServerStreamUseHttp(request, callOptions);

                for (int i = 0; i < responses.Count; ++i)
                {
                    if (i == 0)
                    {
                        ShowPlayers(responses[i].Players, responses[i].Teams);
                    }
                    else
                    {
                        AppendPlayers(responses[i].Players, responses[i].Teams);
                    }
                }
            }
            else
            {
                using (var call = m_Client.SearchPlayer_ServerStream(request, callOptions))
                {
                    bool firstTime = true;

                    await call.ForEachAsync(delegate(PlayerSearchResponse response)
                    {
                        if (firstTime)
                        {
                            firstTime = false;
                            ShowPlayers(response.Players, response.Teams);
                        }
                        else
                        {
                            AppendPlayers(response.Players, response.Teams);
                        }
                    });
                }
            }
        }
        private static void Main(string[] args)
        {
            Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
            var     client  = new PlayerSearch.PlayerSearchClient(channel);

            Console.WriteLine("Help:1,NoStream;2,ServerStream;3,ClientStream;4,DuplexStream");
            var rk = Console.ReadLine();

            while (rk != "exit")
            {
                if (rk == "1")
                {
                    var s1 = client.SearchTeam(new TeamSearchRequest
                    {
                        Country = "ct",
                        Name    = "names"
                    });
                    Console.WriteLine(s1);
                }
                else if (rk == "2")
                {
                    var s2 = client.SearchPlayer_ServerStream(new PlayerSearchRequest
                    {
                        ExpectedDataCount = 10,
                        PlayerName        = "pn",
                        Position          = "1",
                        TeamName          = "tn"
                    });
                    while (s2.ResponseStream.MoveNext(default(CancellationToken)).Result)
                    {
                        Console.WriteLine(s2.ResponseStream.Current);
                    }
                }
                else if (rk == "3")
                {
                    var s2 = client.SearchPlayer_ClientStream();

                    using (var call = client.SearchPlayer_ClientStream())
                    {
                        call.RequestStream.WriteAsync(new PlayerSearchRequest
                        {
                            ExpectedDataCount = 10,
                            PlayerName        = "pn",
                            Position          = "1",
                            TeamName          = "tn"
                        }).Wait();
                        call.RequestStream.WriteAsync(new PlayerSearchRequest
                        {
                            ExpectedDataCount = 10,
                            PlayerName        = "pn",
                            Position          = "1",
                            TeamName          = "tn"
                        }).Wait();
                        call.RequestStream.CompleteAsync().Wait();
                        Console.WriteLine(call.ResponseAsync.Result);
                    }
                }
                else if (rk == "4")
                {
                    using (var call = client.SearchPlayer_DuplexStream())
                    {
                        call.RequestStream.WriteAsync(new PlayerSearchRequest
                        {
                            ExpectedDataCount = 10,
                            PlayerName        = "pn",
                            Position          = "1",
                            TeamName          = "tn"
                        }).Wait();
                        call.RequestStream.WriteAsync(new PlayerSearchRequest
                        {
                            ExpectedDataCount = 10,
                            PlayerName        = "pn",
                            Position          = "1",
                            TeamName          = "tn"
                        }).Wait();
                        call.RequestStream.CompleteAsync().Wait();
                        while (call.ResponseStream.MoveNext(default(CancellationToken)).Result)
                        {
                            Console.WriteLine(call.ResponseStream.Current);
                        }
                        // Console.WriteLine(s2);
                    }
                }
                //else
                {
                    Console.WriteLine("Help:1,NoStream;2,ServerStream;3,ClientStream;4,DuplexStream");
                }
                rk = Console.ReadLine();
            }
        }