Program.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // Set the WebSocket events.
  27. ws.OnOpen += (sender, e) => ws.Send ("Hi, there!");
  28. ws.OnMessage += (sender, e) => {
  29. var fmt = "[WebSocket Message] {0}";
  30. var body = !e.IsPing ? e.Data : "A ping was received.";
  31. Console.WriteLine (fmt, body);
  32. };
  33. ws.OnError += (sender, e) => {
  34. var fmt = "[WebSocket Error] {0}";
  35. Console.WriteLine (fmt, e.Message);
  36. };
  37. ws.OnClose += (sender, e) => {
  38. var fmt = "[WebSocket Close ({0})] {1}";
  39. Console.WriteLine (fmt, e.Code, e.Reason);
  40. };
  41. #if DEBUG
  42. // To change the logging level.
  43. ws.Log.Level = LogLevel.Trace;
  44. // To change the wait time for the response to the Ping or Close.
  45. //ws.WaitTime = TimeSpan.FromSeconds (10);
  46. // To emit a WebSocket.OnMessage event when receives a ping.
  47. //ws.EmitOnPing = true;
  48. #endif
  49. // To enable the Per-message Compression extension.
  50. //ws.Compression = CompressionMethod.Deflate;
  51. // To validate the server certificate.
  52. /*
  53. ws.SslConfiguration.ServerCertificateValidationCallback =
  54. (sender, certificate, chain, sslPolicyErrors) => {
  55. var fmt = "Certificate:\n- Issuer: {0}\n- Subject: {1}";
  56. var msg = String.Format (
  57. fmt, certificate.Issuer, certificate.Subject
  58. );
  59. ws.Log.Debug (msg);
  60. return true; // If the server certificate is valid.
  61. };
  62. */
  63. // To send the credentials for the HTTP Authentication (Basic/Digest).
  64. //ws.SetCredentials ("nobita", "password", false);
  65. // To send the Origin header.
  66. //ws.Origin = "http://localhost:4649";
  67. // To send the cookies.
  68. //ws.SetCookie (new Cookie ("name", "nobita"));
  69. //ws.SetCookie (new Cookie ("roles", "\"idiot, gunfighter\""));
  70. // To connect through the HTTP Proxy server.
  71. //ws.SetProxy ("http://localhost:3128", "nobita", "password");
  72. // To enable the redirection.
  73. //ws.EnableRedirection = true;
  74. // Connect to the server.
  75. ws.Connect ();
  76. // Connect to the server asynchronously.
  77. //ws.ConnectAsync ();
  78. Console.WriteLine ("\nType 'exit' to exit.\n");
  79. while (true) {
  80. Thread.Sleep (1000);
  81. Console.Write ("> ");
  82. var msg = Console.ReadLine ();
  83. if (msg == "exit")
  84. break;
  85. // Send a text message.
  86. ws.Send (msg);
  87. }
  88. }
  89. }
  90. }
  91. }