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.

129 lines
3.9 KiB

13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
12 years ago
13 years ago
5 months ago
5 months ago
12 years ago
12 years ago
13 years ago
12 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
13 years ago
  1. using System;
  2. using System.Threading;
  3. using WebSocketSharp;
  4. using WebSocketSharp.Net;
  5. namespace Example
  6. {
  7. public class Program
  8. {
  9. public static void Main (string[] args)
  10. {
  11. // Create a new instance of the WebSocket class.
  12. //
  13. // The WebSocket class inherits the System.IDisposable interface, so you can
  14. // use the using statement. And the WebSocket connection will be closed with
  15. // close status 1001 (going away) when the control leaves the using block.
  16. //
  17. // If you would like to connect to the server with the secure connection,
  18. // you should create a new instance with a wss scheme WebSocket URL.
  19. using (var ws = new WebSocket ("ws://localhost:4649/Echo"))
  20. //using (var ws = new WebSocket ("wss://localhost:5963/Echo"))
  21. //using (var ws = new WebSocket ("ws://localhost:4649/Chat"))
  22. //using (var ws = new WebSocket ("wss://localhost:5963/Chat"))
  23. //using (var ws = new WebSocket ("ws://localhost:4649/Chat?name=nobita"))
  24. //using (var ws = new WebSocket ("wss://localhost:5963/Chat?name=nobita"))
  25. {
  26. #if DEBUG
  27. // To change the logging level.
  28. ws.Log.Level = LogLevel.Trace;
  29. // To enable the Per-message Compression extension.
  30. //ws.Compression = CompressionMethod.Deflate;
  31. // To emit a WebSocket.OnMessage event when receives a ping.
  32. //ws.EmitOnPing = true;
  33. // To enable the redirection.
  34. //ws.EnableRedirection = true;
  35. // To disable a delay when send or receive buffer of the underlying
  36. // TCP socket is not full.
  37. ws.NoDelay = true;
  38. // To send the Origin header.
  39. //ws.Origin = "http://localhost:4649";
  40. // To send the cookies.
  41. //ws.SetCookie (new Cookie ("name", "nobita"));
  42. //ws.SetCookie (new Cookie ("roles", "\"idiot, gunfighter\""));
  43. // To send the credentials for the HTTP Authentication (Basic/Digest).
  44. //ws.SetCredentials ("nobita", "password", false);
  45. // To connect through the HTTP Proxy server.
  46. //ws.SetProxy ("http://localhost:3128", "nobita", "password");
  47. // To validate the server certificate.
  48. /*
  49. ws.SslConfiguration.ServerCertificateValidationCallback =
  50. (sender, certificate, chain, sslPolicyErrors) => {
  51. var fmt = "Certificate:\n- Issuer: {0}\n- Subject: {1}";
  52. var msg = String.Format (
  53. fmt,
  54. certificate.Issuer,
  55. certificate.Subject
  56. );
  57. ws.Log.Debug (msg);
  58. return true; // If the server certificate is valid.
  59. };
  60. */
  61. // To change the wait time for the response to the Ping or Close.
  62. //ws.WaitTime = TimeSpan.FromSeconds (10);
  63. #endif
  64. // Set the WebSocket events.
  65. ws.OnClose +=
  66. (sender, e) => {
  67. var fmt = "[WebSocket Close ({0})] {1}";
  68. Console.WriteLine (fmt, e.Code, e.Reason);
  69. };
  70. ws.OnError +=
  71. (sender, e) => {
  72. var fmt = "[WebSocket Error] {0}";
  73. Console.WriteLine (fmt, e.Message);
  74. };
  75. ws.OnMessage +=
  76. (sender, e) => {
  77. var fmt = e.IsPing
  78. ? "[WebSocket Ping] {0}"
  79. : "[WebSocket Message] {0}";
  80. Console.WriteLine (fmt, e.Data);
  81. };
  82. ws.OnOpen += (sender, e) => ws.Send ("Hi, there!");
  83. // Connect to the server.
  84. ws.Connect ();
  85. // Connect to the server asynchronously.
  86. //ws.ConnectAsync ();
  87. Console.WriteLine ("\nType \"exit\" to exit.\n");
  88. while (true) {
  89. Thread.Sleep (1000);
  90. Console.Write ("> ");
  91. var msg = Console.ReadLine ();
  92. if (msg == "exit")
  93. break;
  94. // Send a text message.
  95. ws.Send (msg);
  96. }
  97. }
  98. }
  99. }
  100. }