static async Task Main(string[] args) { var userName = UserIntro(); var channel = GrpcChannel.ForAddress("http://localhost:50051"); var client = new Messaging.MessagingClient(channel); using (var chatClient = client.Join()) { var initMessage = new Message { Name = userName }; await chatClient.RequestStream.WriteAsync(initMessage); _ = Task.Run(async() => { await foreach (var message in chatClient.ResponseStream.ReadAllAsync()) { PrintMessage(message); } }); var line = Console.ReadLine(); while (!string.IsNullOrEmpty(line)) { await chatClient.RequestStream.WriteAsync(new Message { Name = userName, Message_ = line }); line = Console.ReadLine(); Console.CursorTop--; } await chatClient.RequestStream.CompleteAsync(); } Console.WriteLine("Disconnected. Press any key to exit."); Console.ReadKey(); }
static async Task <int> Main(string[] args) { var name = "BiDirectionalStreaming-Debug"; //if (args.Length != 1) //{ // Console.WriteLine("No name provided. Using <BiDirectionalStreaming>"); // name = args[0]; //} /// /// Token init /// //HttpClient httpClient = new HttpClient(); //ApiService apiService = new ApiService(httpClient); //var token = await apiService.GetAccessTokenAsync(); //var token = "This is invalid, I hope it fails"; //var tokenValue = "Bearer " + token; //var metadata = new Metadata //{ // { "Authorization", tokenValue } //}; /// /// Call gRPC HTTPS /// //var channelCredentials = new SslCredentials( // File.ReadAllText("Certs\\ca.crt"), // new KeyCertificatePair( // File.ReadAllText("Certs\\client.crt"), // File.ReadAllText("Certs\\client.key") // ) // ); var port = "50051"; var channel = new Channel("localhost:" + port, ChannelCredentials.Insecure);//, channelCredentials); var client = new Messaging.MessagingClient(channel); using (var duplex = client.SendData()) { Console.WriteLine($"Connected as {name}. Send empty message to quit."); // Dispatch, this could be racy var responseTask = Task.Run(async() => { while (await duplex.ResponseStream.MoveNext(CancellationToken.None)) { Console.WriteLine($"{duplex.ResponseStream.Current.Name}: {duplex.ResponseStream.Current.Message}"); } }); var line = Console.ReadLine(); while (!string.IsNullOrEmpty(line)) { await duplex.RequestStream.WriteAsync(new MyMessage { Name = name, Message = line }); line = Console.ReadLine(); } await duplex.RequestStream.CompleteAsync(); } Console.WriteLine("Shutting down"); await channel.ShutdownAsync(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); return(0); }
static async Task <int> Main(string[] args) { var name = "BiDirectionalStreaming"; if (args.Length != 1) { Console.WriteLine("No name provided. Using <BiDirectionalStreaming>"); name = args[0]; } /// /// Token init /// HttpClient httpClient = new HttpClient(); ApiService apiService = new ApiService(httpClient); var token = await apiService.GetAccessTokenAsync(); //var token = "This is invalid, I hope it fails"; var tokenValue = "Bearer " + token; var metadata = new Metadata { { "Authorization", tokenValue } }; var channel = GrpcChannel.ForAddress("https://localhost:50051", new GrpcChannelOptions { HttpClient = CreateHttpClient() }); var client = new Messaging.MessagingClient(channel); using (var duplex = client.SendData(metadata)) { Console.WriteLine($"Connected as {name}. Send empty message to quit."); // Dispatch, this could be racy var responseTask = Task.Run(async() => { while (await duplex.ResponseStream.MoveNext(CancellationToken.None)) { Console.WriteLine($"{duplex.ResponseStream.Current.Name}: {duplex.ResponseStream.Current.Message}"); } }); var line = Console.ReadLine(); while (!string.IsNullOrEmpty(line)) { await duplex.RequestStream.WriteAsync(new MyMessage { Name = name, Message = line }); line = Console.ReadLine(); } await duplex.RequestStream.CompleteAsync(); } Console.WriteLine("Shutting down"); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); return(0); }