Beispiel #1
0
        static void Main(string[] args)
        {
            _ServerIp   = InputString("Server IP:", "localhost", true);
            _ServerPort = InputInteger("Server port:", 9000, true, true);
            _Ssl        = InputBoolean("Use SSL:", false);

            InitializeClient();

            bool runForever = true;

            while (runForever)
            {
                Console.Write("Command [? for help]: ");
                string userInput = Console.ReadLine();
                if (String.IsNullOrEmpty(userInput))
                {
                    continue;
                }

                switch (userInput)
                {
                case "?":
                    Console.WriteLine("Available commands:");
                    Console.WriteLine("  ?            help (this menu)");
                    Console.WriteLine("  q            quit");
                    Console.WriteLine("  cls          clear screen");
                    Console.WriteLine("  send text    send text to the server");
                    Console.WriteLine("  send bytes   send binary data to the server");
                    Console.WriteLine("  sync text    send text to the server and await response");
                    Console.WriteLine("  sync bytes   send binary data to the server and await response");
                    Console.WriteLine("  stats        display client statistics");
                    Console.WriteLine("  status       show if client connected");
                    Console.WriteLine("  dispose      dispose of the connection");
                    Console.WriteLine("  connect      connect to the server if not connected");
                    Console.WriteLine("  reconnect    disconnect if connected, then reconnect");
                    Console.WriteLine("  close        close the connection");
                    break;

                case "q":
                    runForever = false;
                    break;

                case "cls":
                    Console.Clear();
                    break;

                case "send text":
                    Console.Write("Data: ");
                    userInput = Console.ReadLine();
                    if (String.IsNullOrEmpty(userInput))
                    {
                        break;
                    }
                    if (!_Client.SendAsync(userInput).Result)
                    {
                        Console.WriteLine("Failed");
                    }
                    else
                    {
                        Console.WriteLine("Success");
                    }
                    break;

                case "send bytes":
                    Console.Write("Data: ");
                    userInput = Console.ReadLine();
                    if (String.IsNullOrEmpty(userInput))
                    {
                        break;
                    }
                    if (!_Client.SendAsync(Encoding.UTF8.GetBytes(userInput)).Result)
                    {
                        Console.WriteLine("Failed");
                    }
                    break;

                case "sync text":
                    Console.Write("Data: ");
                    userInput = Console.ReadLine();
                    if (String.IsNullOrEmpty(userInput))
                    {
                        break;
                    }
                    string resultStr = _Client.SendAndWaitAsync(userInput).Result;
                    if (!String.IsNullOrEmpty(resultStr))
                    {
                        Console.WriteLine("Response: " + resultStr);
                    }
                    else
                    {
                        Console.WriteLine("(null)");
                    }
                    break;

                case "sync bytes":
                    Console.Write("Data: ");
                    userInput = Console.ReadLine();
                    if (String.IsNullOrEmpty(userInput))
                    {
                        break;
                    }
                    byte[] resultBytes = _Client.SendAndWaitAsync(Encoding.UTF8.GetBytes(userInput)).Result;
                    if (resultBytes != null && resultBytes.Length > 0)
                    {
                        Console.WriteLine("Response: " + Encoding.UTF8.GetString(resultBytes));
                    }
                    else
                    {
                        Console.WriteLine("(null)");
                    }
                    break;

                case "stats":
                    Console.WriteLine(_Client.Stats.ToString());
                    break;

                case "status":
                    if (_Client == null)
                    {
                        Console.WriteLine("Connected: False (null)");
                    }
                    else
                    {
                        Console.WriteLine("Connected: " + _Client.Connected);
                    }
                    break;

                case "dispose":
                    _Client.Dispose();
                    break;

                case "connect":
                    if (_Client != null && _Client.Connected)
                    {
                        Console.WriteLine("Already connected");
                    }
                    else
                    {
                        InitializeClient();
                    }
                    break;

                case "reconnect":
                    InitializeClient();
                    break;

                case "close":
                    _Client.Stop();
                    break;

                default:
                    break;
                }
            }
        }