Program.cs 1.8 KB

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