public async static Task JoinToChatAsync() { AppContext.SetSwitch( "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); var channel = GrpcChannel.ForAddress($"http://{_IP}:5000", new GrpcChannelOptions() { Credentials = ChannelCredentials.Insecure }); var Client = new ChatRoom.ChatRoomClient(channel); chat = Client.join(); await chat.RequestStream.WriteAsync(new Message { NickName = nickname, Msg = $"{nickname} has joined the room" }); while (await chat.ResponseStream.MoveNext(cancellationToken: CancellationToken.None)) { var response = chat.ResponseStream.Current; OnChatRecieve.Invoke(response.NickName, response.Msg); } }
static async Task Main(string[] args) { Console.WriteLine("請輸入使用者姓名"); var userName = Console.ReadLine(); // Include port of the gRPC server as an application argument var port = args.Length > 0 ? args[0] : "50051"; var channel = new Channel("localhost:" + port, ChannelCredentials.Insecure); var client = new ChatRoom.ChatRoomClient(channel); using (var chat = client.join()) { _ = Task.Run(async() => { while (await chat.ResponseStream.MoveNext(cancellationToken: CancellationToken.None)) { var response = chat.ResponseStream.Current; Console.WriteLine($"{response.User}: {response.Text}"); } }); await chat.RequestStream.WriteAsync(new Message { User = userName, Text = $"{userName} has joined the room" }); string line; while ((line = Console.ReadLine()) != null) { if (line.ToLower() == "bye") { break; } await chat.RequestStream.WriteAsync(new Message { User = userName, Text = line }); } await chat.RequestStream.CompleteAsync(); } Console.WriteLine("Disconnecting"); await channel.ShutdownAsync(); }
static async Task Main() { Console.WriteLine("[INFO] Hello World! I am GrpcChatClient!"); Console.Write("Input your user name: "); var userName = Console.ReadLine(); var channel = GrpcChannel.ForAddress("https://localhost:5001"); var client = new ChatRoom.ChatRoomClient(channel); using (var chat = client.join()) { _ = Task.Run(async() => { while (await chat.ResponseStream.MoveNext(cancellationToken: CancellationToken.None)) { var response = chat.ResponseStream.Current; Console.WriteLine($"[{response.User}]: {response.Text}"); } }); await chat.RequestStream.WriteAsync(new Message { User = userName, Text = $"[INFO] {userName} has joined the room." }); string line; while ((line = Console.ReadLine()) != null) { if (line.ToLower() == "bye") { break; } await chat.RequestStream.WriteAsync(new Message { User = userName, Text = line }); } await chat.RequestStream.CompleteAsync(); } Console.WriteLine("[INFO] Disconnecting..."); await channel.ShutdownAsync(); }
public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); var userName = Console.ReadLine(); var port = args.Length > 0 ? args[0] : "50051"; var channel = new Channel("localhost:" + port, ChannelCredentials.Insecure); var client = new ChatRoom.ChatRoomClient(channel); using (var chat = client.join()) { _ = Task.Run(async () => { while (await chat.ResponseStream.MoveNext(cancellationToken: CancellationToken.None)) { var response = chat.ResponseStream.Current; Console.WriteLine($"{response.User}: {response.Text}"); } }); await chat.RequestStream.WriteAsync(new Message { User = userName, Text = $"{userName} has joined the room" }); string line; while ((line = Console.ReadLine()) != null) { if (line.ToLower() == "bye") { break; } await chat.RequestStream.WriteAsync(new Message { User = userName, Text = line }); } await chat.RequestStream.CompleteAsync(); } }