581 lines
41 KiB
HTML
581 lines
41 KiB
HTML
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta http-equiv="X-UA-Compatible" content="chrome=1">
|
|
<title>websocket-sharp by sta</title>
|
|
|
|
<link rel="stylesheet" href="stylesheets/styles.css">
|
|
<link rel="stylesheet" href="stylesheets/pygment_trac.css">
|
|
<script src="javascripts/scale.fix.js"></script>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
|
|
|
|
<!--[if lt IE 9]>
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
</head>
|
|
<body>
|
|
<div class="wrapper">
|
|
<header>
|
|
<h1>websocket-sharp</h1>
|
|
<p>A C# implementation of the WebSocket protocol client and server</p>
|
|
<p class="view"><a href="https://github.com/sta/websocket-sharp">View the Project on GitHub <small>sta/websocket-sharp</small></a></p>
|
|
<ul>
|
|
<li><a href="https://github.com/sta/websocket-sharp/zipball/master">Download <strong>ZIP File</strong></a></li>
|
|
<li><a href="https://github.com/sta/websocket-sharp/tarball/master">Download <strong>TAR Ball</strong></a></li>
|
|
<li><a href="https://github.com/sta/websocket-sharp">View On <strong>GitHub</strong></a></li>
|
|
</ul>
|
|
</header>
|
|
<section>
|
|
<h2>
|
|
<a name="welcome-to-websocket-sharp" class="anchor" href="#welcome-to-websocket-sharp"><span class="octicon octicon-link"></span></a>Welcome to websocket-sharp!</h2>
|
|
|
|
<p><strong>websocket-sharp</strong> supports:</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<strong><a href="#websocket-client">WebSocket Client</a></strong> and <strong><a href="#websocket-server">Server</a></strong>
|
|
</li>
|
|
<li><strong><a href="#supported-websocket-specifications">RFC 6455</a></strong></li>
|
|
<li>
|
|
<strong><a href="#per-message-compression">Per-message Compression</a></strong> extension</li>
|
|
<li><strong><a href="#secure-connection">Secure Connection</a></strong></li>
|
|
<li><strong><a href="#http-authentication">HTTP Authentication</a></strong></li>
|
|
<li>.NET <strong>3.5</strong> or later (includes compatible)</li>
|
|
</ul><h2>
|
|
<a name="build" class="anchor" href="#build"><span class="octicon octicon-link"></span></a>Build</h2>
|
|
|
|
<p>websocket-sharp is built as a single assembly, <strong>websocket-sharp.dll</strong>.</p>
|
|
|
|
<p>websocket-sharp is developed with <strong><a href="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>
|
|
|
|
<h2>
|
|
<a name="install" class="anchor" href="#install"><span class="octicon octicon-link"></span></a>Install</h2>
|
|
|
|
<h3>
|
|
<a name="self-build" class="anchor" href="#self-build"><span class="octicon octicon-link"></span></a>Self Build</h3>
|
|
|
|
<p>You should add <strong>websocket-sharp.dll</strong> (e.g. /path/to/websocket-sharp/bin/Debug/websocket-sharp.dll) built yourself to the library references of your project.</p>
|
|
|
|
<p>If you would like to use that websocket-sharp.dll in your <strong><a href="http://unity3d.com">Unity</a></strong> project, you should add that dll to any folder of your project (e.g. Assets/Plugins) in the <strong>Unity Editor</strong>.</p>
|
|
|
|
<h3>
|
|
<a name="nuget-gallery" class="anchor" href="#nuget-gallery"><span class="octicon octicon-link"></span></a>NuGet Gallery</h3>
|
|
|
|
<p>websocket-sharp is available on the <strong><a href="http://www.nuget.org">NuGet Gallery</a></strong>, as still a <strong>prerelease</strong> version.</p>
|
|
|
|
<ul>
|
|
<li><strong><a href="http://www.nuget.org/packages/WebSocketSharp">NuGet Gallery: websocket-sharp</a></strong></li>
|
|
</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>
|
|
|
|
<pre><code>PM> Install-Package WebSocketSharp -Pre
|
|
</code></pre>
|
|
|
|
<h3>
|
|
<a name="unity-asset-store" class="anchor" href="#unity-asset-store"><span class="octicon octicon-link"></span></a>Unity Asset Store</h3>
|
|
|
|
<p>websocket-sharp is available on the <strong>Unity Asset Store</strong>.</p>
|
|
|
|
<ul>
|
|
<li><strong><a href="http://u3d.as/content/sta-blockhead/websocket-sharp-for-unity">WebSocket-Sharp for Unity</a></strong></li>
|
|
</ul><p>It's priced at <strong>US$15</strong>. I think your $15 makes this project more better and accelerated, <strong>Thank you!</strong></p>
|
|
|
|
<h2>
|
|
<a name="usage" class="anchor" href="#usage"><span class="octicon octicon-link"></span></a>Usage</h2>
|
|
|
|
<h3>
|
|
<a name="websocket-client" class="anchor" href="#websocket-client"><span class="octicon octicon-link"></span></a>WebSocket Client</h3>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="k">using</span> <span class="nn">System</span><span class="p">;</span>
|
|
<span class="k">using</span> <span class="nn">WebSocketSharp</span><span class="p">;</span>
|
|
|
|
<span class="k">namespace</span> <span class="nn">Example</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">class</span> <span class="nc">Program</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> <span class="nf">Main</span> <span class="p">(</span><span class="kt">string</span> <span class="p">[]</span> <span class="n">args</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="k">using</span> <span class="p">(</span><span class="kt">var</span> <span class="n">ws</span> <span class="p">=</span> <span class="k">new</span> <span class="n">WebSocket</span> <span class="p">(</span><span class="s">"ws://dragonsnest.far/Laputa"</span><span class="p">))</span> <span class="p">{</span>
|
|
<span class="n">ws</span><span class="p">.</span><span class="n">OnMessage</span> <span class="p">+=</span> <span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span> <span class="p">=></span>
|
|
<span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span> <span class="p">(</span><span class="s">"Laputa says: "</span> <span class="p">+</span> <span class="n">e</span><span class="p">.</span><span class="n">Data</span><span class="p">);</span>
|
|
|
|
<span class="n">ws</span><span class="p">.</span><span class="n">Connect</span> <span class="p">();</span>
|
|
<span class="n">ws</span><span class="p">.</span><span class="n">Send</span> <span class="p">(</span><span class="s">"BALUS"</span><span class="p">);</span>
|
|
<span class="n">Console</span><span class="p">.</span><span class="n">ReadKey</span> <span class="p">(</span><span class="k">true</span><span class="p">);</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
|
|
<h4>
|
|
<a name="step-1" class="anchor" href="#step-1"><span class="octicon octicon-link"></span></a>Step 1</h4>
|
|
|
|
<p>Required namespace.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="k">using</span> <span class="nn">WebSocketSharp</span><span class="p">;</span>
|
|
</pre></div>
|
|
|
|
<p>The <code>WebSocket</code> class exists in the <code>WebSocketSharp</code> namespace.</p>
|
|
|
|
<h4>
|
|
<a name="step-2" class="anchor" href="#step-2"><span class="octicon octicon-link"></span></a>Step 2</h4>
|
|
|
|
<p>Creating an instance of the <code>WebSocket</code> class with the WebSocket URL to connect.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="k">using</span> <span class="p">(</span><span class="kt">var</span> <span class="n">ws</span> <span class="p">=</span> <span class="k">new</span> <span class="n">WebSocket</span> <span class="p">(</span><span class="s">"ws://example.com"</span><span class="p">))</span> <span class="p">{</span>
|
|
<span class="p">...</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
|
|
<p>The <code>WebSocket</code> class inherits the <code>System.IDisposable</code> interface, so you can use the <code>using</code> statement.</p>
|
|
|
|
<h4>
|
|
<a name="step-3" class="anchor" href="#step-3"><span class="octicon octicon-link"></span></a>Step 3</h4>
|
|
|
|
<p>Setting the <code>WebSocket</code> events.</p>
|
|
|
|
<h5>
|
|
<a name="websocketonopen-event" class="anchor" href="#websocketonopen-event"><span class="octicon octicon-link"></span></a>WebSocket.OnOpen Event</h5>
|
|
|
|
<p>A <code>WebSocket.OnOpen</code> event occurs when the WebSocket connection has been established.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="n">ws</span><span class="p">.</span><span class="n">OnOpen</span> <span class="p">+=</span> <span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span> <span class="p">=></span> <span class="p">{</span>
|
|
<span class="p">...</span>
|
|
<span class="p">};</span>
|
|
</pre></div>
|
|
|
|
<p><code>e</code> has passed as the <code>System.EventArgs.Empty</code>, so you don't use <code>e</code>.</p>
|
|
|
|
<h5>
|
|
<a name="websocketonmessage-event" class="anchor" href="#websocketonmessage-event"><span class="octicon octicon-link"></span></a>WebSocket.OnMessage Event</h5>
|
|
|
|
<p>A <code>WebSocket.OnMessage</code> event occurs when the <code>WebSocket</code> receives a message.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="n">ws</span><span class="p">.</span><span class="n">OnMessage</span> <span class="p">+=</span> <span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span> <span class="p">=></span> <span class="p">{</span>
|
|
<span class="p">...</span>
|
|
<span class="p">};</span>
|
|
</pre></div>
|
|
|
|
<p><code>e</code> has passed as a <code>WebSocketSharp.MessageEventArgs</code>.</p>
|
|
|
|
<p><code>e.Type</code> property returns either <code>WebSocketSharp.Opcode.TEXT</code> or <code>WebSocketSharp.Opcode.BINARY</code> that represents the type of the received message. So by checking it, you determine which item you should use.</p>
|
|
|
|
<p>If <code>e.Type</code> is <code>Opcode.TEXT</code>, you should use <code>e.Data</code> property (returns a <code>string</code>) that represents the received <strong>Text</strong> message.</p>
|
|
|
|
<p>Or if <code>e.Type</code> is <code>Opcode.BINARY</code>, you should use <code>e.RawData</code> property (returns a <code>byte []</code>) that represents the received <strong>Binary</strong> message.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="k">if</span> <span class="p">(</span><span class="n">e</span><span class="p">.</span><span class="n">Type</span> <span class="p">==</span> <span class="n">Opcode</span><span class="p">.</span><span class="n">TEXT</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="c1">// Do something with e.Data</span>
|
|
<span class="k">return</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="k">if</span> <span class="p">(</span><span class="n">e</span><span class="p">.</span><span class="n">Type</span> <span class="p">==</span> <span class="n">Opcode</span><span class="p">.</span><span class="n">BINARY</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="c1">// Do something with e.RawData</span>
|
|
<span class="k">return</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
|
|
<h5>
|
|
<a name="websocketonerror-event" class="anchor" href="#websocketonerror-event"><span class="octicon octicon-link"></span></a>WebSocket.OnError Event</h5>
|
|
|
|
<p>A <code>WebSocket.OnError</code> event occurs when the <code>WebSocket</code> gets an error.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="n">ws</span><span class="p">.</span><span class="n">OnError</span> <span class="p">+=</span> <span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span> <span class="p">=></span> <span class="p">{</span>
|
|
<span class="p">...</span>
|
|
<span class="p">};</span>
|
|
</pre></div>
|
|
|
|
<p><code>e</code> has passed as a <code>WebSocketSharp.ErrorEventArgs</code>.</p>
|
|
|
|
<p><code>e.Message</code> property returns a <code>string</code> that represents the error message. So you should use it to get the error message.</p>
|
|
|
|
<h5>
|
|
<a name="websocketonclose-event" class="anchor" href="#websocketonclose-event"><span class="octicon octicon-link"></span></a>WebSocket.OnClose Event</h5>
|
|
|
|
<p>A <code>WebSocket.OnClose</code> event occurs when the WebSocket connection has been closed.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="n">ws</span><span class="p">.</span><span class="n">OnClose</span> <span class="p">+=</span> <span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span> <span class="p">=></span> <span class="p">{</span>
|
|
<span class="p">...</span>
|
|
<span class="p">};</span>
|
|
</pre></div>
|
|
|
|
<p><code>e</code> has passed as a <code>WebSocketSharp.CloseEventArgs</code>.</p>
|
|
|
|
<p><code>e.Code</code> property returns a <code>ushort</code> that represents the status code indicating the reason for closure, and <code>e.Reason</code> property returns a <code>string</code> that represents the reason for closure. So you should use them to get the reason for closure.</p>
|
|
|
|
<h4>
|
|
<a name="step-4" class="anchor" href="#step-4"><span class="octicon octicon-link"></span></a>Step 4</h4>
|
|
|
|
<p>Connecting to the WebSocket server.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="n">ws</span><span class="p">.</span><span class="n">Connect</span> <span class="p">();</span>
|
|
</pre></div>
|
|
|
|
<p>If you would like to connect to the server asynchronously, you should use the <code>WebSocket.ConnectAsync ()</code> method.</p>
|
|
|
|
<h4>
|
|
<a name="step-5" class="anchor" href="#step-5"><span class="octicon octicon-link"></span></a>Step 5</h4>
|
|
|
|
<p>Sending a data to the WebSocket server.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="n">ws</span><span class="p">.</span><span class="n">Send</span> <span class="p">(</span><span class="n">data</span><span class="p">);</span>
|
|
</pre></div>
|
|
|
|
<p>The <code>WebSocket.Send</code> method is overloaded.</p>
|
|
|
|
<p>You can use the <code>WebSocket.Send (string)</code>, <code>WebSocket.Send (byte [])</code>, and <code>WebSocket.Send (System.IO.FileInfo)</code> methods to send a data.</p>
|
|
|
|
<p>If you would like to send a data asynchronously, you should use the <code>WebSocket.SendAsync</code> method.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="n">ws</span><span class="p">.</span><span class="n">SendAsync</span> <span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">completed</span><span class="p">);</span>
|
|
</pre></div>
|
|
|
|
<p>And if you would like to do something when the send is complete, you should set <code>completed</code> to any <code>Action<bool></code>.</p>
|
|
|
|
<h4>
|
|
<a name="step-6" class="anchor" href="#step-6"><span class="octicon octicon-link"></span></a>Step 6</h4>
|
|
|
|
<p>Closing the WebSocket connection.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="n">ws</span><span class="p">.</span><span class="n">Close</span> <span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">reason</span><span class="p">);</span>
|
|
</pre></div>
|
|
|
|
<p>If you would like to close the connection explicitly, you should use the <code>WebSocket.Close</code> method.</p>
|
|
|
|
<p>The <code>WebSocket.Close</code> method is overloaded.</p>
|
|
|
|
<p>You can use the <code>WebSocket.Close ()</code>, <code>WebSocket.Close (ushort)</code>, <code>WebSocket.Close (WebSocketSharp.CloseStatusCode)</code>, <code>WebSocket.Close (ushort, string)</code>, or <code>WebSocket.Close (WebSocketSharp.CloseStatusCode, string)</code> method to close the connection.</p>
|
|
|
|
<p>If you would like to close the connection asynchronously, you should use the <code>WebSocket.CloseAsync</code> method.</p>
|
|
|
|
<h3>
|
|
<a name="websocket-server" class="anchor" href="#websocket-server"><span class="octicon octicon-link"></span></a>WebSocket Server</h3>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="k">using</span> <span class="nn">System</span><span class="p">;</span>
|
|
<span class="k">using</span> <span class="nn">WebSocketSharp</span><span class="p">;</span>
|
|
<span class="k">using</span> <span class="nn">WebSocketSharp.Server</span><span class="p">;</span>
|
|
|
|
<span class="k">namespace</span> <span class="nn">Example</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">class</span> <span class="nc">Laputa</span> <span class="p">:</span> <span class="n">WebSocketService</span>
|
|
<span class="p">{</span>
|
|
<span class="k">protected</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">OnMessage</span> <span class="p">(</span><span class="n">MessageEventArgs</span> <span class="n">e</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="kt">var</span> <span class="n">msg</span> <span class="p">=</span> <span class="n">e</span><span class="p">.</span><span class="n">Data</span> <span class="p">==</span> <span class="s">"BALUS"</span>
|
|
<span class="p">?</span> <span class="s">"I've been balused already..."</span>
|
|
<span class="p">:</span> <span class="s">"I'm not available now."</span><span class="p">;</span>
|
|
|
|
<span class="n">Send</span> <span class="p">(</span><span class="n">msg</span><span class="p">);</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="k">public</span> <span class="k">class</span> <span class="nc">Program</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> <span class="nf">Main</span> <span class="p">(</span><span class="kt">string</span> <span class="p">[]</span> <span class="n">args</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="kt">var</span> <span class="n">wssv</span> <span class="p">=</span> <span class="k">new</span> <span class="n">WebSocketServer</span> <span class="p">(</span><span class="s">"ws://dragonsnest.far"</span><span class="p">);</span>
|
|
<span class="n">wssv</span><span class="p">.</span><span class="n">AddWebSocketService</span><span class="p"><</span><span class="n">Laputa</span><span class="p">></span> <span class="p">(</span><span class="s">"/Laputa"</span><span class="p">);</span>
|
|
<span class="n">wssv</span><span class="p">.</span><span class="n">Start</span> <span class="p">();</span>
|
|
<span class="n">Console</span><span class="p">.</span><span class="n">ReadKey</span> <span class="p">(</span><span class="k">true</span><span class="p">);</span>
|
|
<span class="n">wssv</span><span class="p">.</span><span class="n">Stop</span> <span class="p">();</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
|
|
<h4>
|
|
<a name="step-1-1" class="anchor" href="#step-1-1"><span class="octicon octicon-link"></span></a>Step 1</h4>
|
|
|
|
<p>Required namespace.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="k">using</span> <span class="nn">WebSocketSharp.Server</span><span class="p">;</span>
|
|
</pre></div>
|
|
|
|
<p>The <code>WebSocketServer</code> and <code>WebSocketService</code> classes exist in the <code>WebSocketSharp.Server</code> namespace.</p>
|
|
|
|
<h4>
|
|
<a name="step-2-1" class="anchor" href="#step-2-1"><span class="octicon octicon-link"></span></a>Step 2</h4>
|
|
|
|
<p>Creating the class that inherits the <code>WebSocketService</code> class.</p>
|
|
|
|
<p>For example, if you would like to provide an echo service,</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="k">using</span> <span class="nn">System</span><span class="p">;</span>
|
|
<span class="k">using</span> <span class="nn">WebSocketSharp</span><span class="p">;</span>
|
|
<span class="k">using</span> <span class="nn">WebSocketSharp.Server</span><span class="p">;</span>
|
|
|
|
<span class="k">public</span> <span class="k">class</span> <span class="nc">Echo</span> <span class="p">:</span> <span class="n">WebSocketService</span>
|
|
<span class="p">{</span>
|
|
<span class="k">protected</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">OnMessage</span> <span class="p">(</span><span class="n">MessageEventArgs</span> <span class="n">e</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="n">Send</span> <span class="p">(</span><span class="n">e</span><span class="p">.</span><span class="n">Data</span><span class="p">);</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
|
|
<p>And if you would like to provide a chat service,</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="k">using</span> <span class="nn">System</span><span class="p">;</span>
|
|
<span class="k">using</span> <span class="nn">WebSocketSharp</span><span class="p">;</span>
|
|
<span class="k">using</span> <span class="nn">WebSocketSharp.Server</span><span class="p">;</span>
|
|
|
|
<span class="k">public</span> <span class="k">class</span> <span class="nc">Chat</span> <span class="p">:</span> <span class="n">WebSocketService</span>
|
|
<span class="p">{</span>
|
|
<span class="k">private</span> <span class="kt">string</span> <span class="n">_suffix</span><span class="p">;</span>
|
|
|
|
<span class="k">public</span> <span class="nf">Chat</span> <span class="p">()</span>
|
|
<span class="p">:</span> <span class="k">this</span> <span class="p">(</span><span class="k">null</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="k">public</span> <span class="nf">Chat</span> <span class="p">(</span><span class="kt">string</span> <span class="n">suffix</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="n">_suffix</span> <span class="p">=</span> <span class="n">suffix</span> <span class="p">??</span> <span class="n">String</span><span class="p">.</span><span class="n">Empty</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="k">protected</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">OnMessage</span> <span class="p">(</span><span class="n">MessageEventArgs</span> <span class="n">e</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="n">Sessions</span><span class="p">.</span><span class="n">Broadcast</span> <span class="p">(</span><span class="n">e</span><span class="p">.</span><span class="n">Data</span> <span class="p">+</span> <span class="n">_suffix</span><span class="p">);</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
|
|
<p>If you override the <code>WebSocketService.OnMessage (MessageEventArgs)</code> method, it's called when the <code>OnMessage</code> event of the <code>WebSocket</code> used by the current session in the WebSocket service occurs.</p>
|
|
|
|
<p>And if you override the <code>WebSocketService.OnOpen ()</code>, <code>WebSocketService.OnError (ErrorEventArgs)</code>, and <code>WebSocketService.OnClose (CloseEventArgs)</code> methods, each of them is called when each event of the <code>WebSocket</code> (the <code>OnOpen</code>, <code>OnError</code>, and <code>OnClose</code>) occurs.</p>
|
|
|
|
<p>The <code>WebSocketService.Send</code> method sends a data to the client on the current session in the WebSocket service.</p>
|
|
|
|
<p>If you would like to access the sessions in the WebSocket service, you should use the <code>WebSocketService.Sessions</code> property (returns a <code>WebSocketSharp.Server.WebSocketSessionManager</code>).</p>
|
|
|
|
<p>The <code>WebSocketService.Sessions.Broadcast</code> method broadcasts a data to every client in the WebSocket service.</p>
|
|
|
|
<h4>
|
|
<a name="step-3-1" class="anchor" href="#step-3-1"><span class="octicon octicon-link"></span></a>Step 3</h4>
|
|
|
|
<p>Creating an instance of the <code>WebSocketServer</code> class.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="kt">var</span> <span class="n">wssv</span> <span class="p">=</span> <span class="k">new</span> <span class="n">WebSocketServer</span> <span class="p">(</span><span class="m">4649</span><span class="p">);</span>
|
|
<span class="n">wssv</span><span class="p">.</span><span class="n">AddWebSocketService</span><span class="p"><</span><span class="n">Echo</span><span class="p">></span> <span class="p">(</span><span class="s">"/Echo"</span><span class="p">);</span>
|
|
<span class="n">wssv</span><span class="p">.</span><span class="n">AddWebSocketService</span><span class="p"><</span><span class="n">Chat</span><span class="p">></span> <span class="p">(</span><span class="s">"/Chat"</span><span class="p">);</span>
|
|
<span class="n">wssv</span><span class="p">.</span><span class="n">AddWebSocketService</span><span class="p"><</span><span class="n">Chat</span><span class="p">></span> <span class="p">(</span><span class="s">"/ChatWithNiceBoat"</span><span class="p">,</span> <span class="p">()</span> <span class="p">=></span> <span class="k">new</span> <span class="n">Chat</span> <span class="p">(</span><span class="s">" Nice boat."</span><span class="p">));</span>
|
|
</pre></div>
|
|
|
|
<p>You can add any WebSocket service to your <code>WebSocketServer</code> with the specified path to the service, using the <code>WebSocketServer.AddWebSocketService<TWithNew> (string)</code> or <code>WebSocketServer.AddWebSocketService<T> (string, Func<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 the <code>WebSocketService</code> class.</p>
|
|
|
|
<p>So you can use the classes created in <strong>Step 2</strong> to add the WebSocket service.</p>
|
|
|
|
<p>If you create an instance of the <code>WebSocketServer</code> class without a port number, the <code>WebSocketServer</code> set the port number to <strong>80</strong> automatically. So it's necessary to run with root permission.</p>
|
|
|
|
<pre><code>$ sudo mono example2.exe
|
|
</code></pre>
|
|
|
|
<h4>
|
|
<a name="step-4-1" class="anchor" href="#step-4-1"><span class="octicon octicon-link"></span></a>Step 4</h4>
|
|
|
|
<p>Starting the WebSocket server.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="n">wssv</span><span class="p">.</span><span class="n">Start</span> <span class="p">();</span>
|
|
</pre></div>
|
|
|
|
<h4>
|
|
<a name="step-5-1" class="anchor" href="#step-5-1"><span class="octicon octicon-link"></span></a>Step 5</h4>
|
|
|
|
<p>Stopping the WebSocket server.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="n">wssv</span><span class="p">.</span><span class="n">Stop</span> <span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">reason</span><span class="p">);</span>
|
|
</pre></div>
|
|
|
|
<p>The <code>WebSocketServer.Stop</code> method is overloaded.</p>
|
|
|
|
<p>You can use the <code>WebSocketServer.Stop ()</code>, <code>WebSocketServer.Stop (ushort, string)</code>, or <code>WebSocketServer.Stop (WebSocketSharp.CloseStatusCode, string)</code> method to stop the server.</p>
|
|
|
|
<h3>
|
|
<a name="http-server-with-the-websocket" class="anchor" href="#http-server-with-the-websocket"><span class="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 <strong><a href="http://www.mono-project.com">Mono</a></strong> to create the HTTP server that allows to accept the WebSocket connection requests.</p>
|
|
|
|
<p>So websocket-sharp provides the <code>WebSocketSharp.Server.HttpServer</code> class.</p>
|
|
|
|
<p>You can add any WebSocket service to your <code>HttpServer</code> with the specified path to the service, using the <code>HttpServer.AddWebSocketService<TWithNew> (string)</code> or <code>HttpServer.AddWebSocketService<T> (string, Func<T>)</code> method.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="kt">var</span> <span class="n">httpsv</span> <span class="p">=</span> <span class="k">new</span> <span class="n">HttpServer</span> <span class="p">(</span><span class="m">4649</span><span class="p">);</span>
|
|
<span class="n">httpsv</span><span class="p">.</span><span class="n">AddWebSocketService</span><span class="p"><</span><span class="n">Echo</span><span class="p">></span> <span class="p">(</span><span class="s">"/Echo"</span><span class="p">);</span>
|
|
<span class="n">httpsv</span><span class="p">.</span><span class="n">AddWebSocketService</span><span class="p"><</span><span class="n">Chat</span><span class="p">></span> <span class="p">(</span><span class="s">"/Chat"</span><span class="p">);</span>
|
|
<span class="n">httpsv</span><span class="p">.</span><span class="n">AddWebSocketService</span><span class="p"><</span><span class="n">Chat</span><span class="p">></span> <span class="p">(</span><span class="s">"/ChatWithNiceBoat"</span><span class="p">,</span> <span class="p">()</span> <span class="p">=></span> <span class="k">new</span> <span class="n">Chat</span> <span class="p">(</span><span class="s">" Nice boat."</span><span class="p">));</span>
|
|
</pre></div>
|
|
|
|
<p>For more information, could you see <strong><a href="https://github.com/sta/websocket-sharp/tree/master/Example3">Example3</a></strong>?</p>
|
|
|
|
<h3>
|
|
<a name="websocket-extensions" class="anchor" href="#websocket-extensions"><span class="octicon octicon-link"></span></a>WebSocket Extensions</h3>
|
|
|
|
<h4>
|
|
<a name="per-message-compression" class="anchor" href="#per-message-compression"><span class="octicon octicon-link"></span></a>Per-message Compression</h4>
|
|
|
|
<p>websocket-sharp supports the <strong><a href="http://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-09">Per-message Compression</a></strong> extension. (But it doesn't support with the <a href="http://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-09#section-8.1">extension parameters</a>.)</p>
|
|
|
|
<p>If you would like to enable this extension as a WebSocket client, you should set like the following.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="n">ws</span><span class="p">.</span><span class="n">Compression</span> <span class="p">=</span> <span class="n">CompressionMethod</span><span class="p">.</span><span class="n">DEFLATE</span><span class="p">;</span>
|
|
</pre></div>
|
|
|
|
<p>And then your client sends the following header with the connection request to the server.</p>
|
|
|
|
<pre><code>Sec-WebSocket-Extensions: permessage-deflate
|
|
</code></pre>
|
|
|
|
<p>If the server supports this extension, it returns the same header. And when your client receives that header, it enables this extension.</p>
|
|
|
|
<h3>
|
|
<a name="secure-connection" class="anchor" href="#secure-connection"><span class="octicon octicon-link"></span></a>Secure Connection</h3>
|
|
|
|
<p>websocket-sharp supports the <strong>Secure Connection (SSL)</strong>.</p>
|
|
|
|
<p>As a <strong>WebSocket Client</strong>, you should create an instance of the <code>WebSocket</code> class with the <strong>wss</strong> scheme WebSocket URL to connect.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="k">using</span> <span class="p">(</span><span class="kt">var</span> <span class="n">ws</span> <span class="p">=</span> <span class="k">new</span> <span class="n">WebSocket</span> <span class="p">(</span><span class="s">"wss://example.com"</span><span class="p">))</span> <span class="p">{</span>
|
|
<span class="p">...</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
|
|
<p>And if you would like to set the custom validation for the server certificate, you should set the <code>WebSocket.ServerCertificateValidationCallback</code> property.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="n">ws</span><span class="p">.</span><span class="n">ServerCertificateValidationCallback</span> <span class="p">=</span> <span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="n">certificate</span><span class="p">,</span> <span class="n">chain</span><span class="p">,</span> <span class="n">sslPolicyErrors</span><span class="p">)</span> <span class="p">=></span> <span class="p">{</span>
|
|
<span class="c1">// Do something to validate the server certificate.</span>
|
|
<span class="k">return</span> <span class="k">true</span><span class="p">;</span> <span class="c1">// If the server certificate is valid.</span>
|
|
<span class="p">};</span>
|
|
</pre></div>
|
|
|
|
<p>If you set this property to nothing, the validation does nothing with the server certificate and returns valid.</p>
|
|
|
|
<p>As a <strong>WebSocket Server</strong>, you should create an instance of the <code>WebSocketServer</code> or <code>HttpServer</code> class with some settings for the secure connection. It's like the following.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="kt">var</span> <span class="n">wssv</span> <span class="p">=</span> <span class="k">new</span> <span class="n">WebSocketServer</span> <span class="p">(</span><span class="m">4649</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span>
|
|
<span class="n">wssv</span><span class="p">.</span><span class="n">Certificate</span> <span class="p">=</span> <span class="k">new</span> <span class="n">X509Certificate2</span> <span class="p">(</span><span class="s">"/path/to/cert.pfx"</span><span class="p">,</span> <span class="s">"password for cert.pfx"</span><span class="p">);</span>
|
|
</pre></div>
|
|
|
|
<h3>
|
|
<a name="http-authentication" class="anchor" href="#http-authentication"><span class="octicon octicon-link"></span></a>HTTP Authentication</h3>
|
|
|
|
<p>websocket-sharp supports the <strong><a href="http://tools.ietf.org/html/rfc2617">HTTP Authentication (Basic/Digest)</a></strong>.</p>
|
|
|
|
<p>As a <strong>WebSocket Client</strong>, you should set a pair of user name and password for the HTTP authentication, using the <code>WebSocket.SetCredentials (string, string, bool)</code> method before connecting.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="n">ws</span><span class="p">.</span><span class="n">SetCredentials</span> <span class="p">(</span><span class="n">username</span><span class="p">,</span> <span class="n">password</span><span class="p">,</span> <span class="n">preAuth</span><span class="p">);</span>
|
|
</pre></div>
|
|
|
|
<p>If <code>preAuth</code> is <code>true</code>, the <code>WebSocket</code> sends the Basic authentication credentials with the first connection request to the server.</p>
|
|
|
|
<p>Or if <code>preAuth</code> is <code>false</code>, the <code>WebSocket</code> sends either the Basic or Digest authentication (determined by the unauthorized response to the first connection request) credentials with the second connection request to the server.</p>
|
|
|
|
<p>As a <strong>WebSocket Server</strong>, you should set an HTTP authentication scheme, a realm, and any function to find the user credentials before starting. It's like the following.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="n">wssv</span><span class="p">.</span><span class="n">AuthenticationSchemes</span> <span class="p">=</span> <span class="n">AuthenticationSchemes</span><span class="p">.</span><span class="n">Basic</span><span class="p">;</span>
|
|
<span class="n">wssv</span><span class="p">.</span><span class="n">Realm</span> <span class="p">=</span> <span class="s">"WebSocket Test"</span><span class="p">;</span>
|
|
<span class="n">wssv</span><span class="p">.</span><span class="n">UserCredentialsFinder</span> <span class="p">=</span> <span class="n">identity</span> <span class="p">=></span> <span class="p">{</span>
|
|
<span class="kt">var</span> <span class="n">expected</span> <span class="p">=</span> <span class="s">"nobita"</span><span class="p">;</span>
|
|
<span class="k">return</span> <span class="n">identity</span><span class="p">.</span><span class="n">Name</span> <span class="p">==</span> <span class="n">expected</span>
|
|
<span class="p">?</span> <span class="k">new</span> <span class="n">NetworkCredential</span> <span class="p">(</span><span class="n">expected</span><span class="p">,</span> <span class="s">"password"</span><span class="p">,</span> <span class="s">"gunfighter"</span><span class="p">)</span> <span class="c1">// User name, password, and roles</span>
|
|
<span class="p">:</span> <span class="k">null</span><span class="p">;</span> <span class="c1">// If the user credentials not found.</span>
|
|
<span class="p">};</span>
|
|
</pre></div>
|
|
|
|
<p>If you would like to provide the Digest authentication, you should set like the following.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="n">wssv</span><span class="p">.</span><span class="n">AuthenticationSchemes</span> <span class="p">=</span> <span class="n">AuthenticationSchemes</span><span class="p">.</span><span class="n">Digest</span><span class="p">;</span>
|
|
</pre></div>
|
|
|
|
<h3>
|
|
<a name="logging" class="anchor" href="#logging"><span class="octicon octicon-link"></span></a>Logging</h3>
|
|
|
|
<p>The <code>WebSocket</code> class includes the own logging function.</p>
|
|
|
|
<p>You can access it with the <code>WebSocket.Log</code> property (returns a <code>WebSocketSharp.Logger</code>).</p>
|
|
|
|
<p>So if you would like to change the current logging level (<code>WebSocketSharp.LogLevel.ERROR</code> as the default), you should set the <code>WebSocket.Log.Level</code> property to any of the <code>LogLevel</code> enum values.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="n">ws</span><span class="p">.</span><span class="n">Log</span><span class="p">.</span><span class="n">Level</span> <span class="p">=</span> <span class="n">LogLevel</span><span class="p">.</span><span class="n">DEBUG</span><span class="p">;</span>
|
|
</pre></div>
|
|
|
|
<p>This means a log with less than <code>LogLevel.DEBUG</code> cannot be outputted.</p>
|
|
|
|
<p>And if you would like to output a log, you should use any of the output methods. The following outputs a log with <code>LogLevel.DEBUG</code>.</p>
|
|
|
|
<div class="highlight highlight-cs"><pre><span class="n">ws</span><span class="p">.</span><span class="n">Log</span><span class="p">.</span><span class="n">Debug</span> <span class="p">(</span><span class="s">"This is a debug message."</span><span class="p">);</span>
|
|
</pre></div>
|
|
|
|
<p>The <code>WebSocketServer</code> and <code>HttpServer</code> classes include the same logging function.</p>
|
|
|
|
<h2>
|
|
<a name="examples" class="anchor" href="#examples"><span class="octicon octicon-link"></span></a>Examples</h2>
|
|
|
|
<p>Examples using <strong>websocket-sharp</strong>.</p>
|
|
|
|
<h3>
|
|
<a name="example" class="anchor" href="#example"><span class="octicon octicon-link"></span></a>Example</h3>
|
|
|
|
<p><strong><a href="https://github.com/sta/websocket-sharp/tree/master/Example">Example</a></strong> connects to the <strong><a href="http://www.websocket.org/echo.html">Echo server</a></strong> with the WebSocket.</p>
|
|
|
|
<h3>
|
|
<a name="example1" class="anchor" href="#example1"><span class="octicon octicon-link"></span></a>Example1</h3>
|
|
|
|
<p><strong><a href="https://github.com/sta/websocket-sharp/tree/master/Example1">Example1</a></strong> connects to the <strong><a href="http://agektmr.node-ninja.com:3000">Audio Data delivery server</a></strong> with the WebSocket. (But it's only implemented the chat feature, still unfinished.)</p>
|
|
|
|
<p>And Example1 uses <strong><a href="http://james.newtonking.com/projects/json-net.aspx">Json.NET</a></strong>.</p>
|
|
|
|
<h3>
|
|
<a name="example2" class="anchor" href="#example2"><span class="octicon octicon-link"></span></a>Example2</h3>
|
|
|
|
<p><strong><a href="https://github.com/sta/websocket-sharp/tree/master/Example2">Example2</a></strong> starts a WebSocket server.</p>
|
|
|
|
<h3>
|
|
<a name="example3" class="anchor" href="#example3"><span class="octicon octicon-link"></span></a>Example3</h3>
|
|
|
|
<p><strong><a href="https://github.com/sta/websocket-sharp/tree/master/Example3">Example3</a></strong> starts an HTTP server that allows to accept the WebSocket connection requests.</p>
|
|
|
|
<p>Could you access to <a href="http://localhost:4649">http://localhost:4649</a> to do <strong>WebSocket Echo Test</strong> with your web browser after Example3 running?</p>
|
|
|
|
<h2>
|
|
<a name="supported-websocket-specifications" class="anchor" href="#supported-websocket-specifications"><span class="octicon octicon-link"></span></a>Supported WebSocket Specifications</h2>
|
|
|
|
<p>websocket-sharp supports <strong><a href="http://tools.ietf.org/html/rfc6455">RFC 6455</a></strong> and is based on the following WebSocket references.</p>
|
|
|
|
<ul>
|
|
<li><strong><a href="http://tools.ietf.org/html/rfc6455">The WebSocket Protocol</a></strong></li>
|
|
<li><strong><a href="http://www.w3.org/TR/websockets">The WebSocket API</a></strong></li>
|
|
<li><strong><a href="http://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-09">Compression Extensions for WebSocket</a></strong></li>
|
|
</ul><p>Thanks for translating to japanese.</p>
|
|
|
|
<ul>
|
|
<li><strong><a href="http://www.hcn.zaq.ne.jp/___/WEB/RFC6455-ja.html">The WebSocket Protocol 日本語訳</a></strong></li>
|
|
<li><strong><a href="http://www.hcn.zaq.ne.jp/___/WEB/WebSocket-ja.html">The WebSocket API 日本語訳</a></strong></li>
|
|
</ul><h2>
|
|
<a name="license" class="anchor" href="#license"><span class="octicon octicon-link"></span></a>License</h2>
|
|
|
|
<p>websocket-sharp is provided under <strong><a href="https://raw.github.com/sta/websocket-sharp/master/LICENSE.txt">The MIT License</a></strong>.</p>
|
|
</section>
|
|
</div>
|
|
<footer>
|
|
<p>Project maintained by <a href="https://github.com/sta">sta</a></p>
|
|
<p>Hosted on GitHub Pages — Theme by <a href="https://github.com/orderedlist">orderedlist</a></p>
|
|
</footer>
|
|
<!--[if !IE]><script>fixScale(document);</script><![endif]-->
|
|
<script type="text/javascript">
|
|
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
|
|
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
|
|
</script>
|
|
<script type="text/javascript">
|
|
try {
|
|
var pageTracker = _gat._getTracker("UA-9752433-2");
|
|
pageTracker._trackPageview();
|
|
} catch(err) {}
|
|
</script>
|
|
|
|
</body>
|
|
</html> |