<strong><ahref="https://github.com/sta/websocket-sharp/tree/master">master</a></strong>: Main branch.</li>
<li>
<strong><ahref="https://github.com/sta/websocket-sharp/tree/hybi-00">hybi-00</a></strong>: A branch for older <ahref="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00">draft-ietf-hybi-thewebsocketprotocol-00</a>. No longer maintained.</li>
<li>
<strong><ahref="https://github.com/sta/websocket-sharp/tree/draft75">draft75</a></strong>: A branch for even more old <ahref="http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75">draft-hixie-thewebsocketprotocol-75</a>. No longer maintained.</li>
<p><strong>websocket-sharp</strong> is built as a single assembly, <strong>websocket-sharp.dll</strong>.</p>
<p>websocket-sharp is developed with <strong><ahref="http://monodevelop.com">MonoDevelop</a></strong>. So the simple way to build is to open <strong>websocket-sharp.sln</strong> and run build for the websocket-sharp project with any of the build configurations (e.g. Debug) in the MonoDevelop.</p>
<p>You should add <strong>websocket-sharp.dll</strong> (e.g. /path/to/websocket-sharp/bin/Debug/websocket-sharp.dll) that you built it yourself to the library references of your project.</p>
<p>If you use websocket-sharp.dll in your <strong><ahref="http://unity3d.com">Unity</a></strong> project, you should add it to any folder of your project (e.g. Assets/Plugins) in the <strong>Unity Editor</strong>.</p>
<p><strong>websocket-sharp</strong> has now been displayed on the <strong><ahref="http://www.nuget.org">NuGet Gallery</a></strong>, as still a <strong>prerelease</strong> version.</p>
</ul><p>You can add websocket-sharp to your project using the <strong>NuGet Package Manager</strong>, the following command in the <strong>Package Manager Console</strong>.</p>
<p><code>e.Type</code> (<code>WebSocketSharp.MessageEventArgs.Type</code>, its type is <code>WebSocketSharp.Opcode</code>) indicates the type of a received data. So by checking it, you determine which item you should use.</p>
<p>If <code>e.Type</code> equals <code>Opcode.TEXT</code>, you use <code>e.Data</code> (<code>WebSocketSharp.MessageEventArgs.Data</code>, its type is <code>string</code>) that contains a received <strong>Text</strong> data.</p>
<p>If <code>e.Type</code> equals <code>Opcode.BINARY</code>, you use <code>e.RawData</code> (<code>WebSocketSharp.MessageEventArgs.RawData</code>, its type is <code>byte []</code>) that contains a received <strong>Binary</strong> data.</p>
<p><code>e.Message</code> (<code>WebSocketSharp.ErrorEventArgs.Message</code>, its type is <code>string</code>) contains an error message, so you use it.</p>
<p><code>e.Code</code> (<code>WebSocketSharp.CloseEventArgs.Code</code>, its type is <code>ushort</code>) contains a status code indicating the reason for closure and <code>e.Reason</code> (<code>WebSocketSharp.CloseEventArgs.Reason</code>, its type is <code>string</code>) contains the reason for closure, so you use them.</p>
<p>If you want to close the WebSocket connection explicitly, you use the <code>Close</code> method.</p>
<p>The <code>Close</code> method is overloaded.</p>
<p>The types of <code>code</code> are <code>WebSocketSharp.CloseStatusCode</code> and <code>ushort</code>, and the type of <code>reason</code> is <code>string</code>.</p>
<p>In addition, the <code>Close ()</code> and <code>Close (code)</code> methods exist, too.</p>
<p>If you override the <code>OnMessage</code> method, it is bound to the server side <code>WebSocket.OnMessage</code> event.</p>
<p>In addition, if you override the <code>OnOpen</code>, <code>OnError</code> and <code>OnClose</code> methods, each of them is bound to each server side event of <code>WebSocket.OnOpen</code>, <code>WebSocket.OnError</code> and <code>WebSocket.OnClose</code>.</p>
<p>The <code>WebSocketService.Send</code> method sends a data to the client of the current session to the WebSocket service.</p>
<p>The <code>WebSocketService.Sessions</code> (its type is <code>WebSocketSharp.Server.WebSocketSessionManager</code>) property provides some functions for the sessions to the WebSocket service.</p>
<p>The <code>WebSocketService.Sessions.Broadcast</code> method sends a data to all client of the WebSocket service.</p>
<p>You can add any WebSocket service with the specified path to the service to your <code>WebSocketServer</code> by using the <code>WebSocketServer.AddWebSocketService<TWithNew></code> or <code>WebSocketServer.AddWebSocketService<T></code> method.</p>
<p>The type of <code>TWithNew</code> must inherit the <code>WebSocketService</code> class and must have a public parameterless constructor.</p>
<p>The type of <code>T</code> must inherit <code>WebSocketService</code> class.</p>
<p>So you can use the classes created in <strong>Step 2</strong>.</p>
<p>If you create an instance of the <code>WebSocketServer</code> class without the port number, the <code>WebSocketServer</code> set the port number to <strong>80</strong> automatically. So it is necessary to run with root permission.</p>
<aname="http-server-with-the-websocket"class="anchor"href="#http-server-with-the-websocket"><spanclass="octicon octicon-link"></span></a>HTTP Server with the WebSocket</h3>
<p>I modified the <code>System.Net.HttpListener</code>, <code>System.Net.HttpListenerContext</code> and some other classes of <ahref="http://www.mono-project.com">Mono</a> to create the HTTP server that can upgrade the connection to the WebSocket connection when receives a WebSocket connection request.</p>
<p>You can add any WebSocket service with the specified path to the service to your <code>HttpServer</code> by using the <code>HttpServer.AddWebSocketService<TWithNew></code> or <code>HttpServer.AddWebSocketService<T></code> method.</p>
<p><strong>websocket-sharp</strong> supports <strong><ahref="http://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-09">Per-message Compression</a></strong> extension. (But it doesn't support with <ahref="http://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-09#section-8.1">extension parameters</a>.)</p>
<p>As a <strong>WebSocket Client</strong>, creating an instance of the <code>WebSocket</code> class with the WebSocket URL with <strong>wss</strong> scheme.</p>
<p>If you want to set the custom validation for the server certificate, you use the <code>WebSocket.ServerCertificateValidationCallback</code> property.</p>
<spanclass="c1">// Do something to validate the server certificate.</span>
<spanclass="k">return</span><spanclass="k">true</span><spanclass="p">;</span><spanclass="c1">// The server certificate is valid.</span>
<spanclass="p">};</span>
</pre></div>
<p>If you set this property to nothing, the validation does nothing with the server certificate, always returns valid.</p>
<p>As a <strong>WebSocket Server</strong>, creating an instance of the <code>WebSocketServer</code> or <code>HttpServer</code> class with some settings for the secure connection.</p>
<spanclass="n">wssv</span><spanclass="p">.</span><spanclass="n">Certificate</span><spanclass="p">=</span><spanclass="k">new</span><spanclass="n">X509Certificate2</span><spanclass="p">(</span><spanclass="s">"/path/to/cert.pfx"</span><spanclass="p">,</span><spanclass="s">"password for cert.pfx"</span><spanclass="p">);</span>
<p>The <code>WebSocket</code> class includes own logging functions.</p>
<p>The <code>WebSocket.Log</code> property provides the logging functions.</p>
<p>If you want to change the current logging level (the default is <code>LogLevel.ERROR</code>), you use the <code>WebSocket.Log.Level</code> property.</p>
<p>The above means that the logging outputs with a less than <code>LogLevel.DEBUG</code> are not outputted.</p>
<p>And if you want to output a log, you use any of some output methods. The following outputs a log with <code>LogLevel.DEBUG</code>.</p>
<divclass="highlight highlight-cs"><pre><spanclass="n">ws</span><spanclass="p">.</span><spanclass="n">Log</span><spanclass="p">.</span><spanclass="n">Debug</span><spanclass="p">(</span><spanclass="s">"This is a debug message."</span><spanclass="p">);</span>
</pre></div>
<p>The <code>WebSocketServer</code> and <code>HttpServer</code> classes include the same logging functions.</p>
<p><ahref="https://github.com/sta/websocket-sharp/tree/master/Example">Example</a> connects to the <ahref="http://www.websocket.org/echo.html">Echo server</a> using the WebSocket.</p>
<p><ahref="https://github.com/sta/websocket-sharp/tree/master/Example1">Example1</a> connects to the <ahref="http://agektmr.node-ninja.com:3000">Audio Data delivery server</a> using the WebSocket (<ahref="https://github.com/sta/websocket-sharp/tree/master/Example1">Example1</a> is only implemented the chat feature, still unfinished).</p>
<p><ahref="https://github.com/sta/websocket-sharp/tree/master/Example3">Example3</a> starts an HTTP server that can upgrade the connection to the WebSocket connection.</p>
<p>Could you access to <ahref="http://localhost:4649">http://localhost:4649</a> to do <strong>WebSocket Echo Test</strong> with your web browser after <ahref="https://github.com/sta/websocket-sharp/tree/master/Example3">Example3</a> running?</p>
<p><strong>websocket-sharp</strong> supports <strong><ahref="http://tools.ietf.org/html/rfc6455">RFC 6455</a></strong> and is based on the following WebSocket references.</p>
<p><strong>websocket-sharp</strong> is provided under <strong><ahref="https://raw.github.com/sta/websocket-sharp/master/LICENSE.txt">The MIT License</a></strong>.</p>
<pclass="repo-owner"><ahref="https://github.com/sta/websocket-sharp"></a> is maintained by <ahref="https://github.com/sta">sta</a>.</p>
<p>This page was generated by <ahref="pages.github.com">GitHub Pages</a> using the Architect theme by <ahref="https://twitter.com/jasonlong">Jason Long</a>.</p>