Exemple #1
0
 public ChatClient()
 {
     Events          = new MyCallback();
     Events.OnLogin += Login;
     Events.OnLeave += Leave;
     Client          = new ChatService.ChatServiceClient(new InstanceContext(Events));
 }
Exemple #2
0
        public static async Task Main(string[] args)
        {
            const string Host = "localhost";
            const int    Port = 16842;

            var channel = new Channel($"{Host}:{Port}", ChannelCredentials.Insecure);
            var client  = new LogInRequest();

            Console.WriteLine("------------------------------------");
            Console.WriteLine("----------- Hello there ! ----------");
            Console.WriteLine("------------------------------------");

            do
            {
                Console.WriteLine("\nPlease enter your name: ");
                client.Name = Console.ReadLine();
                Console.WriteLine("\n------------------------------------");
            } while (isValid(client.Name) == false);

            var logIn = new LogInService.LogInServiceClient(channel);

            logIn.logIn(client);

            Console.WriteLine("Welcome " + client.Name + "!");
            Console.WriteLine("------------------------------------");

            var clientMessage = new ChatRequest();

            clientMessage.Name = client.Name;

            do
            {
                do
                {
                    Console.WriteLine("\nPlease enter your message: ");
                    clientMessage.Textmessage = Console.ReadLine();
                    Console.WriteLine("------------------------------------");
                } while (isValid(clientMessage.Textmessage) == false);

                var chat = new ChatService.ChatServiceClient(channel);
                chat.sendMessage(clientMessage);

                var stream = chat.chatStream();
                while (await stream.ResponseStream.MoveNext())
                {
                    var serverMessage = stream.ResponseStream.Current;

                    Console.WriteLine(serverMessage.Name + " says: " + serverMessage.Textmessage);
                    Console.WriteLine("------------------------------------");
                }
            } while (true);

            // Shutdown
            channel.ShutdownAsync().Wait();
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
        static ClientChatImpl()
        {
            // Create a channel
            var channel = new Channel(Host + ":" + Port, ChannelCredentials.Insecure);

            // Create a client with the channel
            _chatService = new ChatService.ChatServiceClient(channel);

            openConnectionServer();
        }
        static async Task Main(string[] args)
        {
            var userName = args?.FirstOrDefault() ?? "Me";

            var channel = new Channel("localhost:5001", ChannelCredentials.Insecure);

            var client = new ChatService.ChatServiceClient(channel);

            using (var call = client.Chat())
            {
                var cancellationSignal = new CancellationTokenSource();

                var cancellationTask = Task.Run(() =>
                {
                    Console.WriteLine("Press any key to stop");
                    Console.ReadKey();

                    cancellationSignal.Cancel();
                });

                await call.RequestStream.WriteAsync(new ChatRequest
                {
                    Request = new ChatMessage
                    {
                        From    = userName,
                        Message = $"Hi there, it's {userName}!"
                    }
                });

                Console.WriteLine("Message sent!");

                while (await call.ResponseStream.MoveNext(CancellationToken.None))
                {
                    var message = call.ResponseStream.Current;
                    Console.WriteLine($"Received message from {message.Response.From}: {message.Response.Message}");
                }

                await cancellationTask;
                await call.RequestStream.CompleteAsync();
            }

            await channel.ShutdownAsync();
        }
Exemple #5
0
        static async Task Main(string[] args)
        {
            var serverAddress = args.Length > 1 ? args[1] : "https://localhost:5001";

            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                // The following statement allows you to call insecure services. To be used only in development environments.
                AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            }

            var channel = GrpcChannel.ForAddress(serverAddress);
            var client  = new Greeter.GreeterClient(channel);

            var greetRequest = new HelloRequest {
                Name = "Uno"
            };

            var reply = await client.SayHelloAsync(greetRequest, deadline : DateTime.UtcNow.AddSeconds(30));

            Console.WriteLine($"Reply: {reply.Message}...");
            Console.ReadKey();

            Console.WriteLine("Client Streaming now....");

            var clientStreamClient = new ClientEvent.ClientEventClient(channel);
            var sent = 0;

            using (var streamCall = clientStreamClient.UploadEvents())
            {
                while (sent < 20)
                {
                    streamCall.RequestStream.WriteAsync(new StringValue {
                        Value = "Some message here"
                    });
                    sent++;
                    await Task.Delay(TimeSpan.FromSeconds(2));
                }
                await streamCall.RequestStream.CompleteAsync();

                var response = await streamCall.ResponseAsync;

                Console.WriteLine($"Call took {response.ElapsedTime} seconds to complete");
                Console.WriteLine($"Call sttaus was {response.CollationStatus}");
            }


            Console.ReadKey();

            Console.WriteLine("Streaming responses now...");

            var pulseClient = new Heartbeat.HeartbeatClient(channel);

            CancellationTokenSource streamCTS = new CancellationTokenSource(TimeSpan.FromSeconds(20));

            while (!streamCTS.IsCancellationRequested)
            {
                AsyncServerStreamingCall <Pulse> response = pulseClient.GetHeartbeatStream(new Empty(), cancellationToken: streamCTS.Token);

                try
                {
                    await foreach (Pulse pulse in response.ResponseStream.ReadAllAsync(cancellationToken: streamCTS.Token))
                    {
                        Console.WriteLine($"[+] {pulse.EventTime} {pulse.Path} {pulse.EventStatus}");
                    }
                }
                catch (RpcException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            Console.ReadKey();

            Console.WriteLine("Bidirectional streaming now...");

            var chatClient = new ChatService.ChatServiceClient(channel);

            using (var call = chatClient.Chat())
            {
                var responseReaderTask = Task.Run(async() =>
                {
                    while (await call.ResponseStream.MoveNext())
                    {
                        var note = call.ResponseStream.Current;
                        Console.WriteLine("Received " + note);
                    }
                });

                string[] dataDump = new string[] {
                    "Sergio Reguilon",
                    "Mendy",
                    "Mason Mount",
                    "Gabriel Heinze",
                    "Gonzalo Higuain"
                };

                foreach (var payload in dataDump)
                {
                    await call.RequestStream.WriteAsync(new StringValue { Value = payload });
                }
                await call.RequestStream.CompleteAsync();

                await responseReaderTask;
            }
        }