Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.1 kB
0
Indexable
Never
using System;
using Grpc.Core;
using System.Threading.Tasks;
using ChatGrpc;

namespace ChatClient
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
            var client = new Chat.ChatClient(channel);

            var user = "Alice";
            var stream = client.Join();

            var responseTask = Task.Run(async () =>
            {
                while (await stream.ResponseStream.MoveNext())
                {
                    var message = stream.ResponseStream.Current;
                    Console.WriteLine($"{message.User}: {message.Text}");
                }
            });

            await stream.RequestStream.WriteAsync(new Message { User = user, Text = "Hello, everyone!" });

            Console.WriteLine("Type your messages:");
            while (true)
            {
                var text = Console.ReadLine();
                await stream.RequestStream.WriteAsync(new Message { User = user, Text = text });
            }
        }
    }
}