Пример #1
0
        private static void Main(string[] args)
        {
            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       send message to server");
                    Console.WriteLine("  sendasync  send message to server asynchronously");
                    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("  psk        set the preshared key");
                    Console.WriteLine("  auth       authenticate using the preshared key");
                    Console.WriteLine("  debug      enable/disable debug (currently " + client.Debug + ")");
                    break;

                case "q":
                    runForever = false;
                    break;

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

                case "send":
                    Console.Write("Data: ");
                    userInput = Console.ReadLine();
                    if (String.IsNullOrEmpty(userInput))
                    {
                        break;
                    }

                    if (!client.Send(Encoding.UTF8.GetBytes(userInput)))
                    {
                        Console.WriteLine("Failed");
                    }
                    break;

                case "sendasync":
                    Console.Write("Data: ");
                    userInput = Console.ReadLine();
                    if (String.IsNullOrEmpty(userInput))
                    {
                        break;
                    }

                    if (!client.SendAsync(Encoding.UTF8.GetBytes(userInput)).Result)
                    {
                        Console.WriteLine("Failed");
                    }
                    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
                    {
                        client = new WatsonTcpClient(serverIp, serverPort);
                        client.ServerConnected    = ServerConnected;
                        client.ServerDisconnected = ServerDisconnected;
                        client.MessageReceived    = MessageReceived;
                        client.Start();
                    }
                    break;

                case "reconnect":
                    ConnectClient();
                    break;

                case "psk":
                    presharedKey = InputString("Preshared key:", "1234567812345678", false);
                    break;

                case "auth":
                    client.Authenticate(presharedKey);
                    break;

                case "debug":
                    client.Debug = !client.Debug;
                    Console.WriteLine("Debug set to: " + client.Debug);
                    break;

                default:
                    break;
                }
            }
        }