Program.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Net;
  3. using websocketserver.Library;
  4. namespace websocketserver
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. // WebSocket server port
  11. int port = 8080;
  12. if (args.Length > 0) {
  13. port = int.Parse(args[0]);
  14. }
  15. Console.WriteLine($"WebSocket server port: {port}");
  16. var server = new ChatServer(IPAddress.Any, port);
  17. Console.Write("Server starting...");
  18. server.Start();
  19. Console.WriteLine(" Done!");
  20. Console.WriteLine("Commands: ");
  21. Console.WriteLine("\tquit: stops the server");
  22. Console.WriteLine("\trestart: restarts the server");
  23. for (;;)
  24. {
  25. string line = Console.ReadLine();
  26. if (string.IsNullOrEmpty(line)) {
  27. continue;
  28. }
  29. if (line == "quit" || line == "exit")
  30. {
  31. break;
  32. }
  33. if (line == "restart")
  34. {
  35. //TODO: broadcast to all users a disconnect
  36. Console.Write("Server restarting...");
  37. server.Restart();
  38. Console.WriteLine(" Done!");
  39. continue;
  40. }
  41. line = "(admin) " + line;
  42. Console.WriteLine(" Done!");
  43. server.MulticastText(line);
  44. }
  45. Console.Write("Server stopping...");
  46. server.Stop();
  47. Console.WriteLine("Done!");
  48. }
  49. }
  50. }