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()); }
public void Stop() { _server.Dispose(); _timer.Stop(); IsOpen = false; }
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; } } }
static void Main(string[] args) { int exitCode = 0; log.Start(); try { CheckConfig(); log.Add($"Starting server ({ typeof(Program).Assembly.GetName().Version})"); serverConfig.Load(); clients = new Dictionary <User, bool>(); InitializeModules(); InitializeServer(); cleaner.Start(); server.Start(); while (ShowAndHandleMenu(Console.ReadLine())) { } } catch (Exception ex) { exitCode = 1; log.Add(ex.ToString(), MessageType.Error); } finally { log.Add("Closing server"); log.Add("Receiving stopped"); stopReceive = true; if (!(server is null)) { log.Add("Waiting for requests to be processed"); while (clients.Any(x => x.Value)) { Thread.Sleep(10); } log.Add("Disconnecting users"); foreach (KeyValuePair <User, bool> user in clients) { server.DisconnectClient(user.Key.IpPort); } server.Dispose(); } cleaner.Stop(); log.Stop(); GC.Collect(); Environment.Exit(exitCode); } }