예제 #1
0
        static void Main(string[] args)
        {
            Console.Write("Server IP        : ");
            serverIp = Console.ReadLine();

            Console.Write("Server Port      : ");
            serverPort = Convert.ToInt32(Console.ReadLine());

            Console.Write("SSL (true/false) : ");
            ssl = Convert.ToBoolean(Console.ReadLine());

            WatsonWsServer server = new WatsonWsServer(serverIp, serverPort, ssl, true, null, ClientConnected, ClientDisconnected, MessageReceived, true);

            bool runForever = true;

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

                List <string> clients;
                string        ipPort;

                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("  list    list clients");
                    Console.WriteLine("  send    send message to client");
                    Console.WriteLine("  kill    disconnect a client");
                    break;

                case "q":
                    runForever = false;
                    break;

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

                case "list":
                    clients = new List <string>(server.ListClients());
                    if (clients != null && clients.Count > 0)
                    {
                        Console.WriteLine("Clients");
                        foreach (string curr in clients)
                        {
                            Console.WriteLine("  " + curr);
                        }
                    }
                    else
                    {
                        Console.WriteLine("None");
                    }
                    break;

                case "send":
                    Console.Write("IP:Port: ");
                    ipPort = Console.ReadLine();
                    Console.Write("Data: ");
                    userInput = Console.ReadLine();
                    if (String.IsNullOrEmpty(userInput))
                    {
                        break;
                    }
                    server.SendAsync(ipPort, Encoding.UTF8.GetBytes(userInput));
                    break;

                case "kill":
                    Console.Write("IP:Port: ");
                    ipPort = Console.ReadLine();
                    server.KillClient(ipPort);
                    break;

                default:
                    break;
                }
            }
        }