Exemple #1
0
        static void Main(string[] args)
        {
            // test1
            _Server = new WatsonWsServer("localhost", 9000, false);
            _Server.ClientConnected    += (s, a) => { /* Console.WriteLine("Client " + a.IpPort + " connected"); */ };
            _Server.ClientDisconnected += (s, a) => { /* Console.WriteLine("Client " + a.IpPort + " disconnected"); */ };
            _Server.MessageReceived    += (s, a) => { /* Console.WriteLine(Encoding.UTF8.GetString(a.Data)); */ };
            _Server.Start();
            Console.WriteLine("Test 1 with server started: " + ClientTask());

            // test2
            Task.Delay(1000).Wait();
            _Server.Stop();
            Console.WriteLine("Test 2 with server stopped: " + ClientTask());

            // test3
            Task.Delay(1000).Wait();
            _Server.Start();
            Console.WriteLine("Test 3 with server restarted: " + ClientTask());

            // test4
            Task.Delay(1000).Wait();
            _Server.Dispose();
            Console.WriteLine("Test 4 with server disposed: " + ClientTask());

            // test5
            Task.Delay(1000).Wait();
            _Server = new WatsonWsServer("localhost", 9000, false);
            _Server.ClientConnected    += (s, a) => { /* Console.WriteLine("Client " + a.IpPort + " connected"); */ };
            _Server.ClientDisconnected += (s, a) => { /* Console.WriteLine("Client " + a.IpPort + " disconnected"); */ };
            _Server.MessageReceived    += (s, a) => { /* Console.WriteLine(Encoding.UTF8.GetString(a.Data)); */ };
            _Server.Start();
            Console.WriteLine("Test 5 with server started: " + ClientTask());
        }
Exemple #2
0
        static void Main(string[] args)
        {
            _ServerIp   = InputString("Server IP:", "localhost", true);
            _ServerPort = InputInteger("Server port:", 9000, true, true);
            _Ssl        = InputBoolean("Use SSL:", false);

            InitializeServer();
            // InitializeServerMultiple();
            Console.WriteLine("Please manually start the server");

            bool runForever = true;

            while (runForever)
            {
                Console.Write("Command [? for help]: ");
                string userInput = Console.ReadLine()?.Trim();
                if (string.IsNullOrEmpty(userInput))
                {
                    continue;
                }
                string[] splitInput = userInput.Split(new string[] { " " }, 2, StringSplitOptions.None);
                string   ipPort     = null;
                bool     success    = false;

                switch (splitInput[0])
                {
                case "?":
                    Console.WriteLine("Available commands:");
                    Console.WriteLine("  ?                            help (this menu)");
                    Console.WriteLine("  q                            quit");
                    Console.WriteLine("  cls                          clear screen");
                    Console.WriteLine("  dispose                      dispose of the server");
                    Console.WriteLine("  reinit                       reinitialize the server");
                    Console.WriteLine("  start                        start accepting new connections (listening: " + _Server.IsListening + ")");
                    Console.WriteLine("  stop                         stop accepting new connections");
                    Console.WriteLine("  list                         list clients");
                    Console.WriteLine("  stats                        display server statistics");
                    Console.WriteLine("  send ip:port text message    send text to client");
                    Console.WriteLine("  send ip:port bytes message   send binary data to client");
                    Console.WriteLine("  kill ip:port                 disconnect a client");
                    break;

                case "q":
                    runForever = false;
                    break;

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

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

                case "reinit":
                    InitializeServer();
                    break;

                case "start":
                    StartServer();
                    break;

                case "stop":
                    _Server.Stop();
                    break;

                case "list":
                    var clients = new List <string>(_Server.ListClients());
                    if (clients.Count > 0)
                    {
                        Console.WriteLine("Clients");
                        foreach (string curr in clients)
                        {
                            Console.WriteLine("  " + curr);
                        }
                    }
                    else
                    {
                        Console.WriteLine("[No clients connected]");
                    }
                    break;

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

                case "send":
                    if (splitInput.Length != 2)
                    {
                        break;
                    }
                    splitInput = splitInput[1].Split(new string[] { " " }, 3, StringSplitOptions.None);
                    if (splitInput.Length != 3)
                    {
                        break;
                    }
                    if (splitInput[0].Equals("last"))
                    {
                        ipPort = _LastIpPort;
                    }
                    else
                    {
                        ipPort = splitInput[0];
                    }
                    if (String.IsNullOrEmpty(splitInput[2]))
                    {
                        break;
                    }
                    if (splitInput[1].Equals("text"))
                    {
                        success = _Server.SendAsync(ipPort, splitInput[2]).Result;
                    }
                    else if (splitInput[1].Equals("bytes"))
                    {
                        byte[] data = Encoding.UTF8.GetBytes(splitInput[2]);
                        success = _Server.SendAsync(ipPort, data).Result;
                    }
                    else
                    {
                        break;
                    }
                    if (!success)
                    {
                        Console.WriteLine("Failed");
                    }
                    else
                    {
                        Console.WriteLine("Success");
                    }
                    break;

                case "kill":
                    if (splitInput.Length != 2)
                    {
                        break;
                    }
                    _Server.DisconnectClient(splitInput[1]);
                    break;

                default:
                    Console.WriteLine("Unknown command: " + userInput);
                    break;
                }
            }
        }