diff --git a/Example/bin/Debug/example.exe b/Example/bin/Debug/example.exe index 4a59b1a8..a322cd55 100755 Binary files a/Example/bin/Debug/example.exe and b/Example/bin/Debug/example.exe differ diff --git a/Example/bin/Debug/example.exe.mdb b/Example/bin/Debug/example.exe.mdb index 7819435a..c7b13516 100644 Binary files a/Example/bin/Debug/example.exe.mdb and b/Example/bin/Debug/example.exe.mdb differ diff --git a/Example/bin/Debug/websocket-sharp.dll b/Example/bin/Debug/websocket-sharp.dll index fb212a45..4ec08615 100755 Binary files a/Example/bin/Debug/websocket-sharp.dll and b/Example/bin/Debug/websocket-sharp.dll differ diff --git a/Example/bin/Debug/websocket-sharp.dll.mdb b/Example/bin/Debug/websocket-sharp.dll.mdb index 09be24e0..64d0f456 100644 Binary files a/Example/bin/Debug/websocket-sharp.dll.mdb and b/Example/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example/bin/Debug_Ubuntu/example.exe b/Example/bin/Debug_Ubuntu/example.exe index fa1341bc..397c19dd 100755 Binary files a/Example/bin/Debug_Ubuntu/example.exe and b/Example/bin/Debug_Ubuntu/example.exe differ diff --git a/Example/bin/Debug_Ubuntu/example.exe.mdb b/Example/bin/Debug_Ubuntu/example.exe.mdb index 0aa87b82..abf40d6e 100644 Binary files a/Example/bin/Debug_Ubuntu/example.exe.mdb and b/Example/bin/Debug_Ubuntu/example.exe.mdb differ diff --git a/Example/bin/Debug_Ubuntu/websocket-sharp.dll b/Example/bin/Debug_Ubuntu/websocket-sharp.dll index 707b5259..b85f326b 100755 Binary files a/Example/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index fab23b0a..00b5deb5 100644 Binary files a/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example/bin/Release/example.exe b/Example/bin/Release/example.exe index 94c298e4..9eeac9a6 100755 Binary files a/Example/bin/Release/example.exe and b/Example/bin/Release/example.exe differ diff --git a/Example/bin/Release/websocket-sharp.dll b/Example/bin/Release/websocket-sharp.dll index a05e5b1d..40cc36da 100755 Binary files a/Example/bin/Release/websocket-sharp.dll and b/Example/bin/Release/websocket-sharp.dll differ diff --git a/Example/bin/Release_Ubuntu/example.exe b/Example/bin/Release_Ubuntu/example.exe index 600f86a5..919b8ac7 100755 Binary files a/Example/bin/Release_Ubuntu/example.exe and b/Example/bin/Release_Ubuntu/example.exe differ diff --git a/Example/bin/Release_Ubuntu/websocket-sharp.dll b/Example/bin/Release_Ubuntu/websocket-sharp.dll index 69987c64..dd1294ac 100755 Binary files a/Example/bin/Release_Ubuntu/websocket-sharp.dll and b/Example/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/Example1/bin/Debug/example1.exe b/Example1/bin/Debug/example1.exe index 4a20fb72..09021728 100755 Binary files a/Example1/bin/Debug/example1.exe and b/Example1/bin/Debug/example1.exe differ diff --git a/Example1/bin/Debug/example1.exe.mdb b/Example1/bin/Debug/example1.exe.mdb index 78c1b887..9659c9ef 100644 Binary files a/Example1/bin/Debug/example1.exe.mdb and b/Example1/bin/Debug/example1.exe.mdb differ diff --git a/Example1/bin/Debug/websocket-sharp.dll b/Example1/bin/Debug/websocket-sharp.dll index fb212a45..4ec08615 100755 Binary files a/Example1/bin/Debug/websocket-sharp.dll and b/Example1/bin/Debug/websocket-sharp.dll differ diff --git a/Example1/bin/Debug/websocket-sharp.dll.mdb b/Example1/bin/Debug/websocket-sharp.dll.mdb index 09be24e0..64d0f456 100644 Binary files a/Example1/bin/Debug/websocket-sharp.dll.mdb and b/Example1/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example1/bin/Debug_Ubuntu/example1.exe b/Example1/bin/Debug_Ubuntu/example1.exe index 5e88dbae..f82d1e81 100755 Binary files a/Example1/bin/Debug_Ubuntu/example1.exe and b/Example1/bin/Debug_Ubuntu/example1.exe differ diff --git a/Example1/bin/Debug_Ubuntu/example1.exe.mdb b/Example1/bin/Debug_Ubuntu/example1.exe.mdb index 15a36cc7..81f5cb64 100644 Binary files a/Example1/bin/Debug_Ubuntu/example1.exe.mdb and b/Example1/bin/Debug_Ubuntu/example1.exe.mdb differ diff --git a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll index 707b5259..b85f326b 100755 Binary files a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index fab23b0a..00b5deb5 100644 Binary files a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example1/bin/Release/example1.exe b/Example1/bin/Release/example1.exe index bf6c54b7..e3484c0c 100755 Binary files a/Example1/bin/Release/example1.exe and b/Example1/bin/Release/example1.exe differ diff --git a/Example1/bin/Release/websocket-sharp.dll b/Example1/bin/Release/websocket-sharp.dll index a05e5b1d..40cc36da 100755 Binary files a/Example1/bin/Release/websocket-sharp.dll and b/Example1/bin/Release/websocket-sharp.dll differ diff --git a/Example1/bin/Release_Ubuntu/example1.exe b/Example1/bin/Release_Ubuntu/example1.exe index e0e1375f..fe696d1c 100755 Binary files a/Example1/bin/Release_Ubuntu/example1.exe and b/Example1/bin/Release_Ubuntu/example1.exe differ diff --git a/Example1/bin/Release_Ubuntu/websocket-sharp.dll b/Example1/bin/Release_Ubuntu/websocket-sharp.dll index 69987c64..dd1294ac 100755 Binary files a/Example1/bin/Release_Ubuntu/websocket-sharp.dll and b/Example1/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/Example2/bin/Debug/example2.exe b/Example2/bin/Debug/example2.exe index 7c79e258..f03f92f9 100755 Binary files a/Example2/bin/Debug/example2.exe and b/Example2/bin/Debug/example2.exe differ diff --git a/Example2/bin/Debug/example2.exe.mdb b/Example2/bin/Debug/example2.exe.mdb index b4549579..62fb6019 100644 Binary files a/Example2/bin/Debug/example2.exe.mdb and b/Example2/bin/Debug/example2.exe.mdb differ diff --git a/Example2/bin/Debug/websocket-sharp.dll b/Example2/bin/Debug/websocket-sharp.dll index fb212a45..4ec08615 100755 Binary files a/Example2/bin/Debug/websocket-sharp.dll and b/Example2/bin/Debug/websocket-sharp.dll differ diff --git a/Example2/bin/Debug/websocket-sharp.dll.mdb b/Example2/bin/Debug/websocket-sharp.dll.mdb index 09be24e0..64d0f456 100644 Binary files a/Example2/bin/Debug/websocket-sharp.dll.mdb and b/Example2/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example2/bin/Debug_Ubuntu/example2.exe b/Example2/bin/Debug_Ubuntu/example2.exe index 9c1df34c..8401954c 100755 Binary files a/Example2/bin/Debug_Ubuntu/example2.exe and b/Example2/bin/Debug_Ubuntu/example2.exe differ diff --git a/Example2/bin/Debug_Ubuntu/example2.exe.mdb b/Example2/bin/Debug_Ubuntu/example2.exe.mdb index e14fa9fd..db8aa3bf 100644 Binary files a/Example2/bin/Debug_Ubuntu/example2.exe.mdb and b/Example2/bin/Debug_Ubuntu/example2.exe.mdb differ diff --git a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll index 707b5259..b85f326b 100755 Binary files a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index fab23b0a..00b5deb5 100644 Binary files a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example2/bin/Release/example2.exe b/Example2/bin/Release/example2.exe index d1121fd8..88b9099c 100755 Binary files a/Example2/bin/Release/example2.exe and b/Example2/bin/Release/example2.exe differ diff --git a/Example2/bin/Release/websocket-sharp.dll b/Example2/bin/Release/websocket-sharp.dll index a05e5b1d..40cc36da 100755 Binary files a/Example2/bin/Release/websocket-sharp.dll and b/Example2/bin/Release/websocket-sharp.dll differ diff --git a/Example2/bin/Release_Ubuntu/example2.exe b/Example2/bin/Release_Ubuntu/example2.exe index a406631b..a1f4c91e 100755 Binary files a/Example2/bin/Release_Ubuntu/example2.exe and b/Example2/bin/Release_Ubuntu/example2.exe differ diff --git a/Example2/bin/Release_Ubuntu/websocket-sharp.dll b/Example2/bin/Release_Ubuntu/websocket-sharp.dll index 69987c64..dd1294ac 100755 Binary files a/Example2/bin/Release_Ubuntu/websocket-sharp.dll and b/Example2/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/Example3/bin/Debug/Example3.exe b/Example3/bin/Debug/Example3.exe index 6eb285bb..c68e37ae 100755 Binary files a/Example3/bin/Debug/Example3.exe and b/Example3/bin/Debug/Example3.exe differ diff --git a/Example3/bin/Debug/Example3.exe.mdb b/Example3/bin/Debug/Example3.exe.mdb index 0c9ac7c5..4aa16341 100644 Binary files a/Example3/bin/Debug/Example3.exe.mdb and b/Example3/bin/Debug/Example3.exe.mdb differ diff --git a/Example3/bin/Debug/websocket-sharp.dll b/Example3/bin/Debug/websocket-sharp.dll index fb212a45..4ec08615 100755 Binary files a/Example3/bin/Debug/websocket-sharp.dll and b/Example3/bin/Debug/websocket-sharp.dll differ diff --git a/Example3/bin/Debug/websocket-sharp.dll.mdb b/Example3/bin/Debug/websocket-sharp.dll.mdb index 09be24e0..64d0f456 100644 Binary files a/Example3/bin/Debug/websocket-sharp.dll.mdb and b/Example3/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example3/bin/Debug_Ubuntu/Example3.exe b/Example3/bin/Debug_Ubuntu/Example3.exe index d2c89a43..7e64d5b1 100755 Binary files a/Example3/bin/Debug_Ubuntu/Example3.exe and b/Example3/bin/Debug_Ubuntu/Example3.exe differ diff --git a/Example3/bin/Debug_Ubuntu/Example3.exe.mdb b/Example3/bin/Debug_Ubuntu/Example3.exe.mdb index 27f0fc5c..bd083aad 100644 Binary files a/Example3/bin/Debug_Ubuntu/Example3.exe.mdb and b/Example3/bin/Debug_Ubuntu/Example3.exe.mdb differ diff --git a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll index 707b5259..b85f326b 100755 Binary files a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index fab23b0a..00b5deb5 100644 Binary files a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example3/bin/Release/Example3.exe b/Example3/bin/Release/Example3.exe index 7b1a7111..b4f48b4b 100755 Binary files a/Example3/bin/Release/Example3.exe and b/Example3/bin/Release/Example3.exe differ diff --git a/Example3/bin/Release/websocket-sharp.dll b/Example3/bin/Release/websocket-sharp.dll index a05e5b1d..40cc36da 100755 Binary files a/Example3/bin/Release/websocket-sharp.dll and b/Example3/bin/Release/websocket-sharp.dll differ diff --git a/Example3/bin/Release_Ubuntu/Example3.exe b/Example3/bin/Release_Ubuntu/Example3.exe index 2a94ab23..f425b245 100755 Binary files a/Example3/bin/Release_Ubuntu/Example3.exe and b/Example3/bin/Release_Ubuntu/Example3.exe differ diff --git a/Example3/bin/Release_Ubuntu/websocket-sharp.dll b/Example3/bin/Release_Ubuntu/websocket-sharp.dll index 69987c64..dd1294ac 100755 Binary files a/Example3/bin/Release_Ubuntu/websocket-sharp.dll and b/Example3/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/README.md b/README.md index c2e87003..c51a4831 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # websocket-sharp # -**websocket-sharp** is a C# implementation of WebSocket protocol client & server. +**websocket-sharp** is a C# implementation of the WebSocket protocol client & server. ## Usage ## -### WebSocket Client ### +### The WebSocket client ### #### Step 1 #### @@ -15,11 +15,11 @@ using WebSocketSharp; using WebSocketSharp.Frame; ``` -`WebSocket` class exists in `WebSocketSharp` namespace, WebSocket data frame resources (e.g. `WsFrame` class) exist in `WebSocketSharp.Frame` namespace. +The `WebSocket` class exists in the `WebSocketSharp` namespace, the WebSocket frame resources (e.g. `WsFrame` class) exist in the `WebSocketSharp.Frame` namespace. #### Step 2 #### -Creating a instance of `WebSocket` class. +Creating a instance of the `WebSocket` class. ```cs using (WebSocket ws = new WebSocket("ws://example.com")) @@ -28,15 +28,15 @@ using (WebSocket ws = new WebSocket("ws://example.com")) } ``` -`WebSocket` class inherits `IDisposable` interface, so you can use `using` statement. +The `WebSocket` class inherits the `IDisposable` interface, so you can use the `using` statement. #### Step 3 #### -Setting `WebSocket` event handlers. +Setting the `WebSocket` events. ##### WebSocket.OnOpen event ##### -`WebSocket.OnOpen` event is emitted immediately after WebSocket connection has been established. +The `WebSocket.OnOpen` event occurs when the WebSocket connection has been established. ```cs ws.OnOpen += (sender, e) => @@ -45,11 +45,11 @@ ws.OnOpen += (sender, e) => }; ``` -`e` has come across as `EventArgs.Empty`, so there is no operation on `e`. +The `e` has come across as the `EventArgs.Empty`, so there is no operation on the `e`. ##### WebSocket.OnMessage event ##### -`WebSocket.OnMessage` event is emitted each time WebSocket data frame is received. +The `WebSocket.OnMessage` event occurs when the `WebSocket` receives a data frame. ```cs ws.OnMessage += (sender, e) => @@ -58,7 +58,7 @@ ws.OnMessage += (sender, e) => }; ``` -**Frame type** of received WebSocket data frame is stored in `e.Type` (`WebSocketSharp.MessageEventArgs.Type`, its type is `WebSocketSharp.Frame.Opcode`), so you check it out and you determine which item you should operate. +The `e.Type` (`WebSocketSharp.MessageEventArgs.Type`, its type is `WebSocketSharp.Frame.Opcode`) contains the **Frame type** of the data frame, so you check it out and you determine which item you should operate. ```cs switch (e.Type) @@ -74,13 +74,13 @@ switch (e.Type) } ``` -If `e.Type` is `Opcode.TEXT`, you operate `e.Data` (`WebSocketSharp.MessageEventArgs.Data`, its type is `string`). +If the `e.Type` is `Opcode.TEXT`, you operate the `e.Data` (`WebSocketSharp.MessageEventArgs.Data`, its type is `string`). -If `e.Type` is `Opcode.BINARY`, you operate `e.RawData` (`WebSocketSharp.MessageEventArgs.RawData`, its type is `byte[]`). +If the `e.Type` is `Opcode.BINARY`, you operate the `e.RawData` (`WebSocketSharp.MessageEventArgs.RawData`, its type is `byte[]`). ##### WebSocket.OnError event ##### -`WebSocket.OnError` event is emitted when some error is occurred. +The `WebSocket.OnError` event occurs when the `WebSocket` gets an error. ```cs ws.OnError += (sender, e) => @@ -88,11 +88,11 @@ ws.OnError += (sender, e) => ... }; ``` -Error message is stored in `e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, its type is `string`), so you operate it. +The `e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, its type is `string`) contains the error message, so you operate it. ##### WebSocket.OnClose event ##### -`WebSocket.OnClose` event is emitted when WebSocket connection is closed. +The `WebSocket.OnClose` event occurs when the `WebSocket` receives a Close frame or the `Close` method is called. ```cs ws.OnClose += (sender, e) => @@ -101,11 +101,12 @@ ws.OnClose += (sender, e) => }; ``` -Close status code is stored in `e.Code` (`WebSocketSharp.CloseEventArgs.Code`, its type is `WebSocketSharp.Frame.CloseStatusCode`) and reason of close is stored in `e.Reason` (`WebSocketSharp.CloseEventArgs.Reason`, its type is `string`), so you operate them. +The `e.Code` (`WebSocketSharp.CloseEventArgs.Code`, its type is `WebSocketSharp.Frame.CloseStatusCode`) contains the close status code +and the `e.Reason` (`WebSocketSharp.CloseEventArgs.Reason`, its type is `string`) contains the reason why closes, so you operate them. #### Step 4 #### -Connecting to server using WebSocket. +Connecting to the WebSocket server. ```cs ws.Connect(); @@ -113,31 +114,31 @@ ws.Connect(); #### Step 5 #### -Sending data. +Sending a data. ```cs ws.Send(data); ``` -`WebSocket.Send` method is overloaded. +The `Send` method is overloaded. -`data` types are `string`, `byte[]` and `FileInfo` class. +The types of `data` are `string`, `byte[]` or `FileInfo` class. #### Step 6 #### -Closing WebSocket connection. +Closing the WebSocket connection. ```cs ws.Close(code, reason); ``` -If you want to close WebSocket connection explicitly, you can use `Close` method. +If you want to close the WebSocket connection explicitly, you can use the `Close` method. -Type of `code` is `WebSocketSharp.Frame.CloseStatusCode`, type of `reason` is `string`. +The type of `code` is `WebSocketSharp.Frame.CloseStatusCode`, the type of `reason` is `string`. -`WebSocket.Close` method is overloaded (In addition `Close()` and `Close(code)` exist). +The `Close` method is overloaded. (In addition, the `Close()` and `Close(code)` methods exist.) -### WebSocket Server ### +### The WebSocket server ### #### Step 1 #### @@ -147,11 +148,11 @@ Required namespace. using WebSocketSharp.Server; ``` -`WebSocketServer`, `WebSocketServer` and `WebSocketService` classes exist in `WebSocketSharp.Server` namespace. +The `WebSocketServer`, `WebSocketServer` and `WebSocketService` classes exist in the `WebSocketSharp.Server` namespace. #### Step 2 #### -Creating a class that inherits `WebSocketService` class. +Creating a class that inherits the `WebSocketService` class. For example, if you want to provide the echo service, @@ -169,7 +170,7 @@ public class Echo : WebSocketService } ``` -For example, if you want to provide the chat service, +Or if you want to provide the chat service, ```cs using System; @@ -185,19 +186,19 @@ public class Chat : WebSocketService } ``` -If you override `onMessage` method, it is bound to server side `WebSocket.OnMessage` event. +If you override the `onMessage` method, it is bound to the server side `WebSocket.OnMessage` event. -In addition, if you override `onOpen`, `onError` and `onClose` methods, each of them is bound to `WebSocket.OnOpen`, `WebSocket.OnError` and `WebSocket.OnClose` events. +In addition, if you override the `onOpen`, `onError` and `onClose` methods, each of them is bound to the `WebSocket.OnOpen`, `WebSocket.OnError` and `WebSocket.OnClose` events. #### Step 3 #### -Creating a instance of `WebSocketServer` class if you want single WebSocket service server. +Creating a instance of the `WebSocketServer` class if you want the single WebSocket service server. ```cs var wssv = new WebSocketServer("ws://example.com:4649"); ``` -Creating a instance of `WebSocketServer` class if you want multi WebSocket service server. +Creating a instance of the `WebSocketServer` class if you want the multi WebSocket service server. ```cs var wssv = new WebSocketServer(4649); @@ -205,22 +206,22 @@ wssv.AddService("/Echo"); wssv.AddService("/Chat"); ``` -You can add to your `WebSocketServer` any WebSocket service and a matching path to that service by using `WebSocketServer.AddService` method. +You can add to your `WebSocketServer` any WebSocket service and a matching path to that service by using the `WebSocketServer.AddService` method. -Type of `T` inherits `WebSocketService` class, so you can use a class that was created in **Step 2**. +The type of `T` inherits `WebSocketService` class, so you can use a class that was created in **Step 2**. -If you create a instance of WebSocket Server without port number, WebSocket Server set **80** or **443** to port number automatically. +If you create a instance of the `WebSocketServer` class without port number, `WebSocketServer` set **80** to port number automatically. So it is necessary to run with root permission. $ sudo mono example2.exe #### Step 4 #### -Setting WebSocketServer event handler. +Setting the event. ##### WebSocketServer<T>.OnError event ##### -`WebSocketServer.OnError` event is emitted when some error is occurred. +The `WebSocketServer.OnError` event occurs when the `WebSocketServer` gets an error. ```cs wssv.OnError += (sender, e) => @@ -229,15 +230,15 @@ wssv.OnError += (sender, e) => }; ``` -Error message is stored in `e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, its type is `string`), so you operate it. +The `e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, its type is `string`) contains the error message, so you operate it. ##### WebSocketServer.OnError event ##### -Same as `WebSocketServer.OnError` event. +Same as the `WebSocketServer.OnError` event. #### Step 5 #### -Starting server. +Starting the server. ```cs wssv.Start(); @@ -245,17 +246,17 @@ wssv.Start(); #### Step 6 #### -Stopping server. +Stopping the server. ```cs wssv.Stop(); ``` -### HTTP Server with WebSocket ### +### The HTTP server with the WebSocket ### -I modified System.Net.HttpListener, System.Net.HttpListenerContext and some others of [Mono] to create the HTTP server that the connection can be upgraded to the WebSocket connection if the HTTP server received a WebSocket request. +I modified the `System.Net.HttpListener`, `System.Net.HttpListenerContext` and some other classes of [Mono] to create the HTTP server that can upgrade the connection to the WebSocket connection when receives a WebSocket request. -You can add to your `HttpServer` any WebSocket service and a matching path to that service by using `HttpServer.AddService` method. +You can add to your `HttpServer` any WebSocket service and a matching path to that service by using the `HttpServer.AddService` method. ```cs var httpsv = new HttpServer(4649); @@ -284,7 +285,7 @@ Examples of using **websocket-sharp**. ### Example3 ### -[Example3] starts the HTTP server that the connection can be upgraded to the WebSocket connection. +[Example3] starts the HTTP server that can upgrade the connection to the WebSocket connection. Please access [http://localhost:4649](http://localhost:4649) to do WebSocket Echo Test with your web browser after [Example3] running. @@ -326,7 +327,7 @@ Licensed under the **[MIT License]**. [MIT License]: http://www.opensource.org/licenses/mit-license.php [Mono]: http://www.mono-project.com/ [RFC 6455]: http://tools.ietf.org/html/rfc6455 -[The WebSocket API]: http://dev.w3.org/html5/websockets +[The WebSocket API]: http://www.w3.org/TR/websockets/ [The WebSocket API 日本語訳]: http://www.hcn.zaq.ne.jp/___/WEB/WebSocket-ja.html [The WebSocket Protocol]: http://tools.ietf.org/html/rfc6455 [The WebSocket Protocol 日本語訳]: http://www.hcn.zaq.ne.jp/___/WEB/RFC6455-ja.html diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index 502b369b..0fba8302 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -518,20 +518,13 @@ namespace WebSocketSharp { public static string ToString(this T[] array, string separater) { - int len; - StringBuilder sb; - - len = array.Length; + var len = array.Length; if (len == 0) - { return String.Empty; - } - sb = new StringBuilder(); + var sb = new StringBuilder(); for (int i = 0; i < len - 1; i++) - { sb.AppendFormat("{0}{1}", array[i].ToString(), separater); - } sb.Append(array[len - 1].ToString()); return sb.ToString(); diff --git a/websocket-sharp/Server/WebSocketService.cs b/websocket-sharp/Server/WebSocketService.cs index 61254c10..df9826fe 100644 --- a/websocket-sharp/Server/WebSocketService.cs +++ b/websocket-sharp/Server/WebSocketService.cs @@ -53,7 +53,17 @@ namespace WebSocketSharp.Server #endregion - #region Properties + #region Protected Property + + protected SessionManager sessions { + get { + return _sessions; + } + } + + #endregion + + #region Public Properties public string ID { get; private set; } public bool IsBound { get; private set; } diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 90f8333b..6578fcd1 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -2,7 +2,7 @@ /** * WebSocket.cs * - * A C# implementation of a WebSocket protocol client. + * A C# implementation of the WebSocket interface. * This code derived from WebSocket.java (http://github.com/adamac/Java-WebSocket-client). * * The MIT License @@ -36,15 +36,22 @@ using System.Diagnostics; using System.IO; using System.Linq; using System.Net.Sockets; +using System.Security.Cryptography; using System.Text; using System.Threading; -using System.Security.Cryptography; using WebSocketSharp.Frame; using WebSocketSharp.Net; using WebSocketSharp.Net.Sockets; namespace WebSocketSharp { + /// + /// Implements the WebSocket interface. + /// + /// + /// The WebSocket class provides methods and properties for two-way communication with a remote host + /// with the WebSocket protocol (RFC 6455). + /// public class WebSocket : IDisposable { #region Private Const Fields @@ -59,11 +66,10 @@ namespace WebSocketSharp { private string _base64key; private HttpListenerContext _baseContext; - private string _binaryType; private WebSocketContext _context; private System.Net.IPEndPoint _endPoint; - private AutoResetEvent _exitMessageLoop; private string _extensions; + private AutoResetEvent _exitMessageLoop; private Object _forClose; private Object _forSend; private bool _isClient; @@ -83,13 +89,11 @@ namespace WebSocketSharp { private WebSocket() { - _binaryType = String.Empty; _extensions = String.Empty; _forClose = new Object(); _forSend = new Object(); _protocol = String.Empty; _readyState = WsState.CONNECTING; - _receivePong = new AutoResetEvent(false); _unTransmittedBuffer = new SynchronizedCollection(); } @@ -136,6 +140,18 @@ namespace WebSocketSharp { #region Public Constructors + /// + /// Initializes a new instance of the class with the specified WebSocket URL and subprotocols. + /// + /// + /// A that contains the WebSocket URL. + /// + /// + /// An array of that contains the WebSocket subprotocols if any. + /// + /// + /// is not valid WebSocket URL. + /// public WebSocket(string url, params string[] protocols) : this() { @@ -151,6 +167,30 @@ namespace WebSocketSharp { _isSecure = uri.Scheme == "wss" ? true : false; } + /// + /// Initializes a new instance of the class with the specified WebSocket URL, OnOpen, OnMessage, OnError, OnClose event handlers and subprotocols. + /// + /// + /// A that contains the WebSocket URL. + /// + /// + /// An OnOpen event handler. + /// + /// + /// An OnMessage event handler. + /// + /// + /// An OnError event handler. + /// + /// + /// An OnClose event handler. + /// + /// + /// An array of that contains the WebSocket subprotocols if any. + /// + /// + /// is not valid WebSocket URL. + /// public WebSocket( string url, EventHandler onOpen, @@ -172,28 +212,43 @@ namespace WebSocketSharp { #region Properties - public string BinaryType { - get { return _binaryType; } - } - + /// + /// Gets the amount of untransmitted data. + /// + /// + /// The number of bytes of untransmitted data. + /// public ulong BufferedAmount { get { - ulong bufferedAmount = 0; - lock (_unTransmittedBuffer.SyncRoot) { + ulong bufferedAmount = 0; foreach (WsFrame frame in _unTransmittedBuffer) bufferedAmount += frame.PayloadLength; - } - return bufferedAmount; + return bufferedAmount; + } } } + /// + /// Gets the extensions selected by the server. + /// + /// + /// A that contains the extensions if any. By default, String.Empty. (Currently this will only ever be the String.Empty.) + /// public string Extensions { - get { return _extensions; } + get { + return _extensions; + } } + /// + /// Gets a value indicating whether a connection is alive. + /// + /// + /// true if the connection is alive; otherwise, false. + /// public bool IsAlive { get { if (_readyState != WsState.OPEN) @@ -203,18 +258,40 @@ namespace WebSocketSharp { } } + /// + /// Gets a value indicating whether a connection is secure. + /// + /// + /// true if the connection is secure; otherwise, false. + /// public bool IsSecure { get { return _isSecure; } } + /// + /// Gets the subprotocol selected by the server. + /// + /// + /// A that contains the subprotocol if any. By default, String.Empty. + /// public string Protocol { - get { return _protocol; } + get { + return _protocol; + } } + /// + /// Gets the state of the connection. + /// + /// + /// A . By default, WsState.CONNECTING. + /// public WsState ReadyState { - get { return _readyState; } + get { + return _readyState; + } private set { _readyState = value; @@ -234,10 +311,24 @@ namespace WebSocketSharp { } } - public SynchronizedCollection UnTransmittedBuffer { - get { return _unTransmittedBuffer; } + /// + /// Gets the untransmitted WebSocket frames. + /// + /// + /// A IList<WsFrame> that contains the untransmitted WebSocket frames. + /// + public IList UnTransmittedBuffer { + get { + return _unTransmittedBuffer; + } } + /// + /// Gets or sets the WebSocket URL. + /// + /// + /// A that contains the WebSocket URL. + /// public Uri Url { get { return _uri; } set { @@ -252,10 +343,25 @@ namespace WebSocketSharp { #region Events - public event EventHandler OnOpen; + /// + /// Occurs when the WebSocket connection has been established. + /// + public event EventHandler OnOpen; + + /// + /// Occurs when the WebSocket receives a data frame. + /// public event EventHandler OnMessage; - public event EventHandler OnError; - public event EventHandler OnClose; + + /// + /// Occurs when the WebSocket gets an error. + /// + public event EventHandler OnError; + + /// + /// Occurs when the WebSocket receives a Close frame or the Close method is called. + /// + public event EventHandler OnClose; #endregion @@ -318,7 +424,6 @@ namespace WebSocketSharp { private void close(CloseStatusCode code, string reason) { var data = new List(((ushort)code).ToBytes(ByteOrder.BIG)); - if (!String.IsNullOrEmpty(reason)) { var buffer = Encoding.UTF8.GetBytes(reason); @@ -947,6 +1052,7 @@ namespace WebSocketSharp { private void startMessageThread() { + _receivePong = new AutoResetEvent(false); _exitMessageLoop = new AutoResetEvent(false); Action messageInvoker = () => { @@ -975,21 +1081,42 @@ namespace WebSocketSharp { #region Public Methods + /// + /// Sends a Close frame and closes the WebSocket connection and releases all associated resources. + /// public void Close() { Close(CloseStatusCode.NORMAL); } + /// + /// Sends a Close frame and closes the WebSocket connection and releases all associated resources. + /// + /// + /// A . + /// public void Close(CloseStatusCode code) { Close(code, String.Empty); } + /// + /// Sends a Close frame and closes the WebSocket connection and releases all associated resources. + /// + /// + /// A . + /// + /// + /// A that contains the reason why closes. + /// public void Close(CloseStatusCode code, string reason) { close(code, reason); } + /// + /// Establishes a connection. + /// public void Connect() { if (_readyState == WsState.OPEN) @@ -1018,32 +1145,74 @@ namespace WebSocketSharp { } } + /// + /// Sends a Close frame and closes the WebSocket connection and releases all associated resources. + /// + /// + /// Call when you are finished using the . The + /// method leaves the in an unusable state. After + /// calling , you must release all references to the so + /// the garbage collector can reclaim the memory that the was occupying. + /// public void Dispose() { Close(CloseStatusCode.AWAY); } + /// + /// Sends a Ping frame. + /// + /// + /// true if the WebSocket receives a Pong frame in a time; otherwise, false. + /// public bool Ping() { return Ping(String.Empty); } + /// + /// Sends a Ping frame with a message. + /// + /// + /// A that contains the message data to be sent. + /// + /// + /// true if the WebSocket receives a Pong frame in a time; otherwise, false. + /// public bool Ping(string data) { return ping(data, 5 * 1000); } + /// + /// Sends a Text data frame. + /// + /// + /// A that contains the text data to be sent. + /// public void Send(string data) { var buffer = Encoding.UTF8.GetBytes(data); send(Opcode.TEXT, buffer); } + /// + /// Sends a Binary data frame. + /// + /// + /// An array of that contains the binary data to be sent. + /// public void Send(byte[] data) { send(Opcode.BINARY, data); } + /// + /// Sends a Binary data frame. + /// + /// + /// A that contains the binary data to be sent. + /// public void Send(FileInfo file) { using (FileStream fs = file.OpenRead()) diff --git a/websocket-sharp/bin/Debug/websocket-sharp.dll b/websocket-sharp/bin/Debug/websocket-sharp.dll index fb212a45..4ec08615 100755 Binary files a/websocket-sharp/bin/Debug/websocket-sharp.dll and b/websocket-sharp/bin/Debug/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb b/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb index 09be24e0..64d0f456 100644 Binary files a/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb and b/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll index 707b5259..b85f326b 100755 Binary files a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll and b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index fab23b0a..00b5deb5 100644 Binary files a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/websocket-sharp/bin/Release/websocket-sharp.dll b/websocket-sharp/bin/Release/websocket-sharp.dll index a05e5b1d..40cc36da 100755 Binary files a/websocket-sharp/bin/Release/websocket-sharp.dll and b/websocket-sharp/bin/Release/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll index 69987c64..dd1294ac 100755 Binary files a/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll and b/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/websocket-sharp/websocket-sharp.pidb b/websocket-sharp/websocket-sharp.pidb index 48ea44ff..438b419c 100644 Binary files a/websocket-sharp/websocket-sharp.pidb and b/websocket-sharp/websocket-sharp.pidb differ