Program.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Configuration;
  3. using System.Security.Cryptography.X509Certificates;
  4. using WebSocketSharp;
  5. using WebSocketSharp.Net;
  6. using WebSocketSharp.Server;
  7. namespace Example2
  8. {
  9. public class Program
  10. {
  11. public static void Main (string[] args)
  12. {
  13. // Create a new instance of the WebSocketServer class.
  14. //
  15. // If you would like to provide the secure connection, you should
  16. // create a new instance with the 'secure' parameter set to true or
  17. // with a wss scheme WebSocket URL.
  18. var wssv = new WebSocketServer (4649);
  19. //var wssv = new WebSocketServer (5963, true);
  20. //var wssv = new WebSocketServer (System.Net.IPAddress.Any, 4649);
  21. //var wssv = new WebSocketServer (System.Net.IPAddress.Any, 5963, true);
  22. //var wssv = new WebSocketServer (System.Net.IPAddress.IPv6Any, 4649);
  23. //var wssv = new WebSocketServer (System.Net.IPAddress.IPv6Any, 5963, true);
  24. //var wssv = new WebSocketServer ("ws://0.0.0.0:4649");
  25. //var wssv = new WebSocketServer ("wss://0.0.0.0:5963");
  26. //var wssv = new WebSocketServer ("ws://[::0]:4649");
  27. //var wssv = new WebSocketServer ("wss://[::0]:5963");
  28. //var wssv = new WebSocketServer (System.Net.IPAddress.Loopback, 4649);
  29. //var wssv = new WebSocketServer (System.Net.IPAddress.Loopback, 5963, true);
  30. //var wssv = new WebSocketServer (System.Net.IPAddress.IPv6Loopback, 4649);
  31. //var wssv = new WebSocketServer (System.Net.IPAddress.IPv6Loopback, 5963, true);
  32. //var wssv = new WebSocketServer ("ws://localhost:4649");
  33. //var wssv = new WebSocketServer ("wss://localhost:5963");
  34. //var wssv = new WebSocketServer ("ws://127.0.0.1:4649");
  35. //var wssv = new WebSocketServer ("wss://127.0.0.1:5963");
  36. //var wssv = new WebSocketServer ("ws://[::1]:4649");
  37. //var wssv = new WebSocketServer ("wss://[::1]:5963");
  38. #if DEBUG
  39. // To change the logging level.
  40. wssv.Log.Level = LogLevel.Trace;
  41. // To change the wait time for the response to the WebSocket Ping or Close.
  42. //wssv.WaitTime = TimeSpan.FromSeconds (2);
  43. // Not to remove the inactive sessions periodically.
  44. //wssv.KeepClean = false;
  45. #endif
  46. // To provide the secure connection.
  47. /*
  48. var cert = ConfigurationManager.AppSettings["ServerCertFile"];
  49. var passwd = ConfigurationManager.AppSettings["CertFilePassword"];
  50. wssv.SslConfiguration.ServerCertificate = new X509Certificate2 (cert, passwd);
  51. */
  52. // To provide the HTTP Authentication (Basic/Digest).
  53. /*
  54. wssv.AuthenticationSchemes = AuthenticationSchemes.Basic;
  55. wssv.Realm = "WebSocket Test";
  56. wssv.UserCredentialsFinder = id => {
  57. var name = id.Name;
  58. // Return user name, password, and roles.
  59. return name == "nobita"
  60. ? new NetworkCredential (name, "password", "gunfighter")
  61. : null; // If the user credentials are not found.
  62. };
  63. */
  64. // To resolve to wait for socket in TIME_WAIT state.
  65. //wssv.ReuseAddress = true;
  66. // Add the WebSocket services.
  67. wssv.AddWebSocketService<Echo> ("/Echo");
  68. wssv.AddWebSocketService<Chat> ("/Chat");
  69. // Add the WebSocket service with initializing.
  70. /*
  71. wssv.AddWebSocketService<Chat> (
  72. "/Chat",
  73. s => {
  74. s.Prefix = "Anon#";
  75. // To send the Sec-WebSocket-Protocol header that has a subprotocol name.
  76. s.Protocol = "chat";
  77. // To ignore the Sec-WebSocket-Extensions header.
  78. s.IgnoreExtensions = true;
  79. // To emit a WebSocket.OnMessage event when receives a ping.
  80. s.EmitOnPing = true;
  81. // To validate the Origin header.
  82. s.OriginValidator = val => {
  83. // Check the value of the Origin header, and return true if valid.
  84. Uri origin;
  85. return !val.IsNullOrEmpty ()
  86. && Uri.TryCreate (val, UriKind.Absolute, out origin)
  87. && origin.Host == "localhost";
  88. };
  89. // To validate the cookies.
  90. s.CookiesValidator = (req, res) => {
  91. // Check the cookies in 'req', and set the cookies to send to
  92. // the client with 'res' if necessary.
  93. foreach (var cookie in req) {
  94. cookie.Expired = true;
  95. res.Add (cookie);
  96. }
  97. return true; // If valid.
  98. };
  99. }
  100. );
  101. */
  102. wssv.Start ();
  103. if (wssv.IsListening) {
  104. Console.WriteLine ("Listening on port {0}, and providing WebSocket services:", wssv.Port);
  105. foreach (var path in wssv.WebSocketServices.Paths)
  106. Console.WriteLine ("- {0}", path);
  107. }
  108. Console.WriteLine ("\nPress Enter key to stop the server...");
  109. Console.ReadLine ();
  110. wssv.Stop ();
  111. }
  112. }
  113. }