You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.2 KiB

12 years ago
3 months ago
4 months ago
12 years ago
12 years ago
  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 provide the HTTP Authentication (Basic/Digest).
  42. /*
  43. wssv.AuthenticationSchemes = AuthenticationSchemes.Basic;
  44. wssv.Realm = "WebSocket Test";
  45. wssv.UserCredentialsFinder =
  46. id => {
  47. var name = id.Name;
  48. // Return user name, password, and roles.
  49. return name == "nobita"
  50. ? new NetworkCredential (name, "password", "gunfighter")
  51. : null; // If the user credentials are not found.
  52. };
  53. */
  54. // To remove the inactive sessions periodically.
  55. //wssv.KeepClean = true;
  56. // To resolve to wait for socket in TIME_WAIT state.
  57. //wssv.ReuseAddress = true;
  58. // To provide the secure connection.
  59. /*
  60. var cert = ConfigurationManager.AppSettings["ServerCertFile"];
  61. var passwd = ConfigurationManager.AppSettings["CertFilePassword"];
  62. wssv.SslConfiguration.ServerCertificate = new X509Certificate2 (
  63. cert,
  64. passwd
  65. );
  66. */
  67. // To change the wait time for the response to the WebSocket Ping or Close.
  68. //wssv.WaitTime = TimeSpan.FromSeconds (2);
  69. #endif
  70. // Add the WebSocket services.
  71. wssv.AddWebSocketService<Echo> ("/Echo");
  72. // With initializing.
  73. wssv.AddWebSocketService<Chat> (
  74. "/Chat",
  75. s => {
  76. s.Prefix = "Anon#";
  77. #if DEBUG
  78. // To validate the cookies.
  79. /*
  80. s.CookiesValidator =
  81. (req, res) => {
  82. // Check the cookies in "req", and set the cookies to send to
  83. // the client with "res" if necessary.
  84. foreach (var cookie in req) {
  85. cookie.Expired = true;
  86. res.Add (cookie);
  87. }
  88. return true; // If valid.
  89. };
  90. */
  91. // To emit a WebSocket.OnMessage event when receives a ping.
  92. //s.EmitOnPing = true;
  93. // To ignore the Sec-WebSocket-Extensions header.
  94. //s.IgnoreExtensions = true;
  95. // To disable a delay when send or receive buffer of the underlying
  96. // TCP socket is not full.
  97. s.NoDelay = true;
  98. // To validate the Origin header.
  99. /*
  100. s.OriginValidator =
  101. val => {
  102. // Check the value of the Origin header, and return true if valid.
  103. Uri origin;
  104. return !val.IsNullOrEmpty ()
  105. && Uri.TryCreate (val, UriKind.Absolute, out origin)
  106. && origin.Host == "localhost";
  107. };
  108. */
  109. // To send the Sec-WebSocket-Protocol header that has a subprotocol
  110. // name.
  111. //s.Protocol = "chat";
  112. #endif
  113. }
  114. );
  115. // Start the server.
  116. wssv.Start ();
  117. if (wssv.IsListening) {
  118. var fmt = "Listening on port {0}, and providing WebSocket services:";
  119. Console.WriteLine (fmt, wssv.Port);
  120. foreach (var path in wssv.WebSocketServices.Paths)
  121. Console.WriteLine ("- {0}", path);
  122. }
  123. Console.WriteLine ("\nPress Enter key to stop the server...");
  124. Console.ReadLine ();
  125. // Stop the server.
  126. wssv.Stop ();
  127. }
  128. }
  129. }