Exemple #1
0
		private static async Task ClientStreamingCallExample(Cltstream.CltstreamClient client)
		{
			using (var call = client.AccumulateCount())
			{
				for (var i = 0; i < 3; i++)
				{
					var count = RNG.Next(5);
					Console.WriteLine($"Accumulating with {count}");
					await call.RequestStream.WriteAsync(new CounterRequest { Count = count });
					await Task.Delay(2000);
				}

				await call.RequestStream.CompleteAsync();

				var response = await call;
				Console.WriteLine($"Count: {response.Count}");
			}
		}
Exemple #2
0
		static async Task Main(string[] args)
		{
			AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

			var channel = GrpcChannel.ForAddress("https://localhost:5001");
			var customerClient = new Customer.CustomerClient(channel);

			#region Unary RCP
			Console.WriteLine("Unary RPC");
			Console.WriteLine("==============================================");
			await EnterCustomerDetail(customerClient);

			Console.WriteLine("Would you like to get any customer detail ? y/n");
			bool isRpt = Convert.ToBoolean((Console.ReadLine().ToLower().Equals("y", StringComparison.InvariantCultureIgnoreCase)) ? true : false);

			if (isRpt)
			{
				await EnterCustomerDetail(customerClient);
			}
			#endregion

			#region Server Streaming RCP

			Console.WriteLine();
			Console.WriteLine("Server Streaming");
			Console.WriteLine("==============================================");
			Console.WriteLine();
			Console.WriteLine("All Customers List :");
			Console.WriteLine();
			using (var call = customerClient.GetNewCustomer(new NewCustomerRequest()))
			{
				while (await call.ResponseStream.MoveNext())
				{
					var currentCustomer = call.ResponseStream.Current;

					Console.WriteLine($"{currentCustomer.FirstName} {currentCustomer.LastName} :- {currentCustomer.EmailAddress}");
				}
			}
			#endregion

			#region Client Streaming RCP
			Console.WriteLine();
			Console.WriteLine("Client Streaming");
			Console.WriteLine("==============================================");


			var streamClient = new Cltstream.CltstreamClient(channel);
			await ClientStreamingCallExample(streamClient);

			#endregion

			#region Bidirectional RCP
			Console.WriteLine();
			Console.WriteLine("Bi-Direactional Streaming");
			Console.WriteLine("==============================================");

			var customer = new CustomerProtoModel
			{
				ColorInConsole = GetRandomChatColor(),
				Id = Guid.NewGuid().ToString(),
				Name = args.Length > 0 ? args[0] : "TheHulk"
			};


			var client = new ChatService.ChatServiceClient(channel);
			var joinCustomerReply = await client.JoinCustomerChatAsync(new JoinCustomerRequest
			{
				Customer = customer
			});

			using (var streaming = client.SendMessageToChatRoom(new Metadata { new Metadata.Entry("CustomerName", customer.Name) }))
			{
				var response = Task.Run(async () =>
				{
					while (await streaming.ResponseStream.MoveNext())
					{
						Console.ForegroundColor = Enum.Parse<ConsoleColor>(streaming.ResponseStream.Current.Color);
						Console.WriteLine($"{streaming.ResponseStream.Current.CustomerName}: {streaming.ResponseStream.Current.Message}");
						Console.ForegroundColor = Enum.Parse<ConsoleColor>(customer.ColorInConsole);
					}
				});

				await streaming.RequestStream.WriteAsync(new ChatMessage
				{
					CustomerId = customer.Id,
					Color = customer.ColorInConsole,
					Message = "",
					RoomId = joinCustomerReply.RoomId,
					CustomerName = customer.Name
				});
				Console.ForegroundColor = Enum.Parse<ConsoleColor>(customer.ColorInConsole);
				Console.WriteLine($"Joined the chat as {customer.Name}");

				var line = Console.ReadLine();
				DeletePrevConsoleLine();
				while (!string.Equals(line, "qw!", StringComparison.OrdinalIgnoreCase))
				{
					await streaming.RequestStream.WriteAsync(new ChatMessage
					{
						Color = customer.ColorInConsole,
						CustomerId = customer.Id,
						CustomerName = customer.Name,
						Message = line,
						RoomId = joinCustomerReply.RoomId
					});
					line = Console.ReadLine();
					DeletePrevConsoleLine();
				}
				await streaming.RequestStream.CompleteAsync();
			}
			Console.WriteLine("Press any key to exit...");
			#endregion


			Console.WriteLine("==========**** END ****===========");
			Console.ReadLine();
		}