Create gh-pages branch via GitHub
This commit is contained in:
parent
b6e0524bec
commit
dbe8ba1a57
526
index.html
526
index.html
@ -39,7 +39,7 @@
|
||||
</div>
|
||||
|
||||
<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>
|
||||
<a id="welcome-to-websocket-sharp" class="anchor" href="#welcome-to-websocket-sharp" aria-hidden="true"><span class="octicon octicon-link"></span></a>Welcome to websocket-sharp!</h2>
|
||||
|
||||
<p><strong>websocket-sharp</strong> supports:</p>
|
||||
|
||||
@ -58,24 +58,24 @@
|
||||
</ul>
|
||||
|
||||
<h2>
|
||||
<a name="build" class="anchor" href="#build"><span class="octicon octicon-link"></span></a>Build</h2>
|
||||
<a id="build" class="anchor" href="#build" aria-hidden="true"><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 <strong>websocket-sharp project</strong> with any of the build configurations (e.g. <code>Debug</code>) in the MonoDevelop.</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 <strong>websocket-sharp project</strong> with any of the build configurations (e.g. <code>Debug</code>) in MonoDevelop.</p>
|
||||
|
||||
<h2>
|
||||
<a name="install" class="anchor" href="#install"><span class="octicon octicon-link"></span></a>Install</h2>
|
||||
<a id="install" class="anchor" href="#install" aria-hidden="true"><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>
|
||||
<a id="self-build" class="anchor" href="#self-build" aria-hidden="true"><span class="octicon octicon-link"></span></a>Self Build</h3>
|
||||
|
||||
<p>You should add <strong>websocket-sharp.dll</strong> (e.g. <code>/path/to/websocket-sharp/bin/Debug/websocket-sharp.dll</code>) built yourself to the library references of your project.</p>
|
||||
<p>You should add your <strong>websocket-sharp.dll</strong> (e.g. <code>/path/to/websocket-sharp/bin/Debug/websocket-sharp.dll</code>) 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. <code>Assets/Plugins</code>) in the <strong>Unity Editor</strong>.</p>
|
||||
<p>If you would like to use that dll in your <strong><a href="http://unity3d.com">Unity</a></strong> project, you should add it to any folder of your project (e.g. <code>Assets/Plugins</code>) in <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>
|
||||
<a id="nuget-gallery" class="anchor" href="#nuget-gallery" aria-hidden="true"><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>
|
||||
|
||||
@ -83,13 +83,13 @@
|
||||
<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>
|
||||
<p>You can add websocket-sharp to your project with the <strong>NuGet Package Manager</strong>, by using 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>
|
||||
<a id="unity-asset-store" class="anchor" href="#unity-asset-store" aria-hidden="true"><span class="octicon octicon-link"></span></a>Unity Asset Store</h3>
|
||||
|
||||
<p>websocket-sharp is available on the <strong>Unity Asset Store</strong>.</p>
|
||||
|
||||
@ -112,81 +112,76 @@
|
||||
<p>And 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>
|
||||
<a id="usage" class="anchor" href="#usage" aria-hidden="true"><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>
|
||||
<a id="websocket-client" class="anchor" href="#websocket-client" aria-hidden="true"><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>
|
||||
<div class="highlight highlight-csharp"><pre><span class="pl-k">using</span> System;
|
||||
<span class="pl-k">using</span> WebSocketSharp;
|
||||
|
||||
<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="pl-k">namespace</span> Example
|
||||
{
|
||||
<span class="pl-s">public</span> <span class="pl-s">class</span> <span class="pl-entc">Program</span>
|
||||
{
|
||||
<span class="pl-s">public</span> <span class="pl-s">static</span> <span class="pl-s">void</span> <span class="pl-enf">Main</span> (<span class="pl-s">string[]</span> <span class="pl-vpf">args</span>)
|
||||
{
|
||||
<span class="pl-ko">using</span> (<span class="pl-k">var</span> ws = <span class="pl-s">new</span> WebSocket (<span class="pl-s1"><span class="pl-pds">"</span>ws://dragonsnest.far/Laputa<span class="pl-pds">"</span></span>)) {
|
||||
ws.OnMessage += (sender, e) =>
|
||||
Console.WriteLine (<span class="pl-s1"><span class="pl-pds">"</span>Laputa says: <span class="pl-pds">"</span></span> + e.Data);
|
||||
|
||||
<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>
|
||||
ws.Connect ();
|
||||
ws.Send (<span class="pl-s1"><span class="pl-pds">"</span>BALUS<span class="pl-pds">"</span></span>);
|
||||
Console.ReadKey (<span class="pl-c1">true</span>);
|
||||
}
|
||||
}
|
||||
}
|
||||
}</pre></div>
|
||||
|
||||
<h4>
|
||||
<a name="step-1" class="anchor" href="#step-1"><span class="octicon octicon-link"></span></a>Step 1</h4>
|
||||
<a id="step-1" class="anchor" href="#step-1" aria-hidden="true"><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>
|
||||
<div class="highlight highlight-csharp"><pre><span class="pl-k">using</span> WebSocketSharp;</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>
|
||||
<a id="step-2" class="anchor" href="#step-2" aria-hidden="true"><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>
|
||||
<p>Creating a new 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>
|
||||
<div class="highlight highlight-csharp"><pre><span class="pl-ko">using</span> (<span class="pl-k">var</span> ws = <span class="pl-s">new</span> WebSocket (<span class="pl-s1"><span class="pl-pds">"</span>ws://example.com<span class="pl-pds">"</span></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. And the WebSocket connection is closed with close status <code>1001</code> (going away) when the control leaves the <code>using</code> block.</p>
|
||||
<p>The <code>WebSocket</code> class inherits the <code>System.IDisposable</code> interface, so you can use the <code>using</code> statement. And the WebSocket connection will be closed with close status <code>1001</code> (going away) when the control leaves the <code>using</code> block.</p>
|
||||
|
||||
<h4>
|
||||
<a name="step-3" class="anchor" href="#step-3"><span class="octicon octicon-link"></span></a>Step 3</h4>
|
||||
<a id="step-3" class="anchor" href="#step-3" aria-hidden="true"><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>
|
||||
<a id="websocketonopen-event" class="anchor" href="#websocketonopen-event" aria-hidden="true"><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>
|
||||
<div class="highlight highlight-csharp"><pre>ws.OnOpen += (sender, e) => {
|
||||
...
|
||||
};</pre></div>
|
||||
|
||||
<p><code>e</code> has passed as the <code>System.EventArgs.Empty</code>, so you don't use <code>e</code>.</p>
|
||||
<p><code>e</code> has passed as the <code>System.EventArgs.Empty</code>, so you don't need to use it.</p>
|
||||
|
||||
<h5>
|
||||
<a name="websocketonmessage-event" class="anchor" href="#websocketonmessage-event"><span class="octicon octicon-link"></span></a>WebSocket.OnMessage Event</h5>
|
||||
<a id="websocketonmessage-event" class="anchor" href="#websocketonmessage-event" aria-hidden="true"><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>
|
||||
<div class="highlight highlight-csharp"><pre>ws.OnMessage += (sender, e) => {
|
||||
...
|
||||
};</pre></div>
|
||||
|
||||
<p><code>e</code> has passed as a <code>WebSocketSharp.MessageEventArgs</code>.</p>
|
||||
|
||||
@ -196,68 +191,63 @@
|
||||
|
||||
<p>Or if it returns <code>Opcode.Binary</code>, you should use <code>e.RawData</code> property that returns a <code>byte[]</code> (represents the <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="p">...</span>
|
||||
<div class="highlight highlight-csharp"><pre><span class="pl-k">if</span> (e.Type == Opcode.Text) {
|
||||
<span class="pl-c">// Do something with e.Data.</span>
|
||||
...
|
||||
|
||||
<span class="k">return</span><span class="p">;</span>
|
||||
<span class="p">}</span>
|
||||
<span class="pl-k">return</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="p">...</span>
|
||||
<span class="pl-k">if</span> (e.Type == Opcode.Binary) {
|
||||
<span class="pl-c">// Do something with e.RawData.</span>
|
||||
...
|
||||
|
||||
<span class="k">return</span><span class="p">;</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
<span class="pl-k">return</span>;
|
||||
}</pre></div>
|
||||
|
||||
<h5>
|
||||
<a name="websocketonerror-event" class="anchor" href="#websocketonerror-event"><span class="octicon octicon-link"></span></a>WebSocket.OnError Event</h5>
|
||||
<a id="websocketonerror-event" class="anchor" href="#websocketonerror-event" aria-hidden="true"><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>
|
||||
<div class="highlight highlight-csharp"><pre>ws.OnError += (sender, e) => {
|
||||
...
|
||||
};</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>
|
||||
|
||||
<p>And if the error is due to an exception, you can get the <code>System.Exception</code> instance that caused the error, by using <code>e.Exception</code> property.</p>
|
||||
<p>And if the error is due to an exception, you can get a <code>System.Exception</code> instance that caused the error, by using <code>e.Exception</code> property.</p>
|
||||
|
||||
<h5>
|
||||
<a name="websocketonclose-event" class="anchor" href="#websocketonclose-event"><span class="octicon octicon-link"></span></a>WebSocket.OnClose Event</h5>
|
||||
<a id="websocketonclose-event" class="anchor" href="#websocketonclose-event" aria-hidden="true"><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>
|
||||
<div class="highlight highlight-csharp"><pre>ws.OnClose += (sender, e) => {
|
||||
...
|
||||
};</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 the close, and <code>e.Reason</code> property returns a <code>string</code> that represents the reason for the close. So you should use them to get the reason for the close.</p>
|
||||
|
||||
<h4>
|
||||
<a name="step-4" class="anchor" href="#step-4"><span class="octicon octicon-link"></span></a>Step 4</h4>
|
||||
<a id="step-4" class="anchor" href="#step-4" aria-hidden="true"><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>
|
||||
<div class="highlight highlight-csharp"><pre>ws.Connect ();</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>
|
||||
<a id="step-5" class="anchor" href="#step-5" aria-hidden="true"><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>
|
||||
<div class="highlight highlight-csharp"><pre>ws.Send (data);</pre></div>
|
||||
|
||||
<p>The <code>WebSocket.Send</code> method is overloaded.</p>
|
||||
|
||||
@ -265,18 +255,16 @@
|
||||
|
||||
<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>
|
||||
<div class="highlight highlight-csharp"><pre>ws.SendAsync (data, completed);</pre></div>
|
||||
|
||||
<p>And also if you would like to do something when the send is complete, you should set <code>completed</code> to any <code>Action<bool></code> delegate.</p>
|
||||
|
||||
<h4>
|
||||
<a name="step-6" class="anchor" href="#step-6"><span class="octicon octicon-link"></span></a>Step 6</h4>
|
||||
<a id="step-6" class="anchor" href="#step-6" aria-hidden="true"><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>
|
||||
<div class="highlight highlight-csharp"><pre>ws.Close (code, reason);</pre></div>
|
||||
|
||||
<p>If you would like to close the connection explicitly, you should use the <code>WebSocket.Close</code> method.</p>
|
||||
|
||||
@ -287,96 +275,92 @@
|
||||
<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>
|
||||
<a id="websocket-server" class="anchor" href="#websocket-server" aria-hidden="true"><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>
|
||||
<div class="highlight highlight-csharp"><pre><span class="pl-k">using</span> System;
|
||||
<span class="pl-k">using</span> WebSocketSharp;
|
||||
<span class="pl-k">using</span> WebSocketSharp.Server;
|
||||
|
||||
<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">WebSocketBehavior</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="pl-k">namespace</span> Example
|
||||
{
|
||||
<span class="pl-s">public</span> <span class="pl-s">class</span> <span class="pl-entc">Laputa</span> : <span class="pl-s">WebSocketBehavior</span>
|
||||
{
|
||||
<span class="pl-s">protected</span> <span class="pl-s">override</span> <span class="pl-s">void</span> <span class="pl-enf">OnMessage</span> (<span class="pl-s">MessageEventArgs</span> <span class="pl-vpf">e</span>)
|
||||
{
|
||||
<span class="pl-k">var</span> msg = e.Data == <span class="pl-s1"><span class="pl-pds">"</span>BALUS<span class="pl-pds">"</span></span>
|
||||
? <span class="pl-s1"><span class="pl-pds">"</span>I've been balused already...<span class="pl-pds">"</span></span>
|
||||
: <span class="pl-s1"><span class="pl-pds">"</span>I'm not available now.<span class="pl-pds">"</span></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>
|
||||
Send (msg);
|
||||
}
|
||||
}
|
||||
|
||||
<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>
|
||||
<span class="pl-s">public</span> <span class="pl-s">class</span> <span class="pl-entc">Program</span>
|
||||
{
|
||||
<span class="pl-s">public</span> <span class="pl-s">static</span> <span class="pl-s">void</span> <span class="pl-enf">Main</span> (<span class="pl-s">string[]</span> <span class="pl-vpf">args</span>)
|
||||
{
|
||||
<span class="pl-k">var</span> wssv = <span class="pl-s">new</span> WebSocketServer (<span class="pl-s1"><span class="pl-pds">"</span>ws://dragonsnest.far<span class="pl-pds">"</span></span>);
|
||||
wssv.AddWebSocketService<Laputa> (<span class="pl-s1"><span class="pl-pds">"</span>/Laputa<span class="pl-pds">"</span></span>);
|
||||
wssv.Start ();
|
||||
Console.ReadKey (<span class="pl-c1">true</span>);
|
||||
wssv.Stop ();
|
||||
}
|
||||
}
|
||||
}</pre></div>
|
||||
|
||||
<h4>
|
||||
<a name="step-1-1" class="anchor" href="#step-1-1"><span class="octicon octicon-link"></span></a>Step 1</h4>
|
||||
<a id="step-1-1" class="anchor" href="#step-1-1" aria-hidden="true"><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>
|
||||
<div class="highlight highlight-csharp"><pre><span class="pl-k">using</span> WebSocketSharp.Server;</pre></div>
|
||||
|
||||
<p>The <code>WebSocketBehavior</code> and <code>WebSocketServer</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>
|
||||
<a id="step-2-1" class="anchor" href="#step-2-1" aria-hidden="true"><span class="octicon octicon-link"></span></a>Step 2</h4>
|
||||
|
||||
<p>Creating the class that inherits the <code>WebSocketBehavior</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>
|
||||
<div class="highlight highlight-csharp"><pre><span class="pl-k">using</span> System;
|
||||
<span class="pl-k">using</span> WebSocketSharp;
|
||||
<span class="pl-k">using</span> WebSocketSharp.Server;
|
||||
|
||||
<span class="k">public</span> <span class="k">class</span> <span class="nc">Echo</span> <span class="p">:</span> <span class="n">WebSocketBehavior</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>
|
||||
<span class="pl-s">public</span> <span class="pl-s">class</span> <span class="pl-entc">Echo</span> : <span class="pl-s">WebSocketBehavior</span>
|
||||
{
|
||||
<span class="pl-s">protected</span> <span class="pl-s">override</span> <span class="pl-s">void</span> <span class="pl-enf">OnMessage</span> (<span class="pl-s">MessageEventArgs</span> <span class="pl-vpf">e</span>)
|
||||
{
|
||||
Send (e.Data);
|
||||
}
|
||||
}</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>
|
||||
<div class="highlight highlight-csharp"><pre><span class="pl-k">using</span> System;
|
||||
<span class="pl-k">using</span> WebSocketSharp;
|
||||
<span class="pl-k">using</span> WebSocketSharp.Server;
|
||||
|
||||
<span class="k">public</span> <span class="k">class</span> <span class="nc">Chat</span> <span class="p">:</span> <span class="n">WebSocketBehavior</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="pl-s">public</span> <span class="pl-s">class</span> <span class="pl-entc">Chat</span> : <span class="pl-s">WebSocketBehavior</span>
|
||||
{
|
||||
<span class="pl-s">private</span> <span class="pl-s">string</span> _suffix;
|
||||
|
||||
<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="pl-s">public</span> <span class="pl-enf">Chat</span> ()
|
||||
: <span class="pl-c1">this</span> (null)
|
||||
{
|
||||
}
|
||||
|
||||
<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="pl-s">public</span> <span class="pl-enf">Chat</span> (<span class="pl-s">string</span> <span class="pl-vpf">suffix</span>)
|
||||
{
|
||||
_suffix = suffix ?? String.Empty;
|
||||
}
|
||||
|
||||
<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>
|
||||
<span class="pl-s">protected</span> <span class="pl-s">override</span> <span class="pl-s">void</span> <span class="pl-enf">OnMessage</span> (<span class="pl-s">MessageEventArgs</span> <span class="pl-vpf">e</span>)
|
||||
{
|
||||
Sessions.Broadcast (e.Data + _suffix);
|
||||
}
|
||||
}</pre></div>
|
||||
|
||||
<p>You can define the behavior of any WebSocket service by creating the class that inherits the <code>WebSocketBehavior</code> class.</p>
|
||||
|
||||
@ -391,17 +375,16 @@
|
||||
<p>The <code>WebSocketBehavior.Sessions.Broadcast</code> method broadcasts a data to every client in the service.</p>
|
||||
|
||||
<h4>
|
||||
<a name="step-3-1" class="anchor" href="#step-3-1"><span class="octicon octicon-link"></span></a>Step 3</h4>
|
||||
<a id="step-3-1" class="anchor" href="#step-3-1" aria-hidden="true"><span class="octicon octicon-link"></span></a>Step 3</h4>
|
||||
|
||||
<p>Creating an instance of the <code>WebSocketServer</code> class.</p>
|
||||
<p>Creating a new 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">"/ChatWithNyan"</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">" Nyan!"</span><span class="p">));</span>
|
||||
</pre></div>
|
||||
<div class="highlight highlight-csharp"><pre><span class="pl-k">var</span> wssv = <span class="pl-s">new</span> WebSocketServer (<span class="pl-cn">4649</span>);
|
||||
wssv.AddWebSocketService<Echo> (<span class="pl-s1"><span class="pl-pds">"</span>/Echo<span class="pl-pds">"</span></span>);
|
||||
wssv.AddWebSocketService<Chat> (<span class="pl-s1"><span class="pl-pds">"</span>/Chat<span class="pl-pds">"</span></span>);
|
||||
wssv.AddWebSocketService<Chat> (<span class="pl-s1"><span class="pl-pds">"</span>/ChatWithNyan<span class="pl-pds">"</span></span>, () => <span class="pl-s">new</span> Chat (<span class="pl-s1"><span class="pl-pds">"</span> Nyan!<span class="pl-pds">"</span></span>));</pre></div>
|
||||
|
||||
<p>You can add any WebSocket service to your <code>WebSocketServer</code> with the specified behavior and path to the service, using the <code>WebSocketServer.AddWebSocketService<TBehaviorWithNew> (string)</code> or <code>WebSocketServer.AddWebSocketService<TBehavior> (string, Func<TBehavior>)</code> method.</p>
|
||||
<p>You can add any WebSocket service to your <code>WebSocketServer</code> with the specified behavior and path to the service, by using the <code>WebSocketServer.AddWebSocketService<TBehaviorWithNew> (string)</code> or <code>WebSocketServer.AddWebSocketService<TBehavior> (string, Func<TBehavior>)</code> method.</p>
|
||||
|
||||
<p>The type of <code>TBehaviorWithNew</code> must inherit the <code>WebSocketBehavior</code> class, and must have a public parameterless constructor.</p>
|
||||
|
||||
@ -409,201 +392,190 @@
|
||||
|
||||
<p>So you can use the classes created in <strong>Step 2</strong> to add the 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>
|
||||
<p>If you create a instance of the <code>WebSocketServer</code> class without a port number, the <code>WebSocketServer</code> class 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>
|
||||
<a id="step-4-1" class="anchor" href="#step-4-1" aria-hidden="true"><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>
|
||||
<div class="highlight highlight-csharp"><pre>wssv.Start ();</pre></div>
|
||||
|
||||
<h4>
|
||||
<a name="step-5-1" class="anchor" href="#step-5-1"><span class="octicon octicon-link"></span></a>Step 5</h4>
|
||||
<a id="step-5-1" class="anchor" href="#step-5-1" aria-hidden="true"><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>
|
||||
<div class="highlight highlight-csharp"><pre>wssv.Stop (code, reason);</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>
|
||||
<a id="http-server-with-the-websocket" class="anchor" href="#http-server-with-the-websocket" aria-hidden="true"><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 behavior and path to the service, using the <code>HttpServer.AddWebSocketService<TBehaviorWithNew> (string)</code> or <code>HttpServer.AddWebSocketService<TBehavior> (string, Func<TBehavior>)</code> method.</p>
|
||||
<p>You can add any WebSocket service to your <code>HttpServer</code> with the specified behavior and path to the service, by using the <code>HttpServer.AddWebSocketService<TBehaviorWithNew> (string)</code> or <code>HttpServer.AddWebSocketService<TBehavior> (string, Func<TBehavior>)</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">"/ChatWithNyan"</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">" Nyan!"</span><span class="p">));</span>
|
||||
</pre></div>
|
||||
<div class="highlight highlight-csharp"><pre><span class="pl-k">var</span> httpsv = <span class="pl-s">new</span> HttpServer (<span class="pl-cn">4649</span>);
|
||||
httpsv.AddWebSocketService<Echo> (<span class="pl-s1"><span class="pl-pds">"</span>/Echo<span class="pl-pds">"</span></span>);
|
||||
httpsv.AddWebSocketService<Chat> (<span class="pl-s1"><span class="pl-pds">"</span>/Chat<span class="pl-pds">"</span></span>);
|
||||
httpsv.AddWebSocketService<Chat> (<span class="pl-s1"><span class="pl-pds">"</span>/ChatWithNyan<span class="pl-pds">"</span></span>, () => <span class="pl-s">new</span> Chat (<span class="pl-s1"><span class="pl-pds">"</span> Nyan!<span class="pl-pds">"</span></span>));</pre></div>
|
||||
|
||||
<p>For more information, would 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>
|
||||
<a id="websocket-extensions" class="anchor" href="#websocket-extensions" aria-hidden="true"><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>
|
||||
<a id="per-message-compression" class="anchor" href="#per-message-compression" aria-hidden="true"><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>
|
||||
<p>If you would like to enable this extension as a WebSocket client, you should set such as 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>
|
||||
<div class="highlight highlight-csharp"><pre>ws.Compression = CompressionMethod.Deflate;</pre></div>
|
||||
|
||||
<p>And then your client sends the following header with the connection request to the server.</p>
|
||||
<p>And then your client will send 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>
|
||||
<p>If the server supports this extension, it will return the same header. And when your client receives it, this extension will be available.</p>
|
||||
|
||||
<h3>
|
||||
<a name="secure-connection" class="anchor" href="#secure-connection"><span class="octicon octicon-link"></span></a>Secure Connection</h3>
|
||||
<a id="secure-connection" class="anchor" href="#secure-connection" aria-hidden="true"><span class="octicon octicon-link"></span></a>Secure Connection</h3>
|
||||
|
||||
<p>websocket-sharp supports the <strong>Secure Connection (SSL)</strong>.</p>
|
||||
<p>websocket-sharp supports the <strong>Secure Connection</strong> with <strong>SSL/TLS</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.</p>
|
||||
<p>As a <strong>WebSocket Client</strong>, you should create a new instance of the <code>WebSocket</code> class with the <strong>wss</strong> scheme WebSocket URL.</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>
|
||||
<div class="highlight highlight-csharp"><pre><span class="pl-ko">using</span> (<span class="pl-k">var</span> ws = <span class="pl-s">new</span> WebSocket (<span class="pl-s1"><span class="pl-pds">"</span>wss://example.com<span class="pl-pds">"</span></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>
|
||||
<p>And if you would like to use the custom validation for the server certificate, you should set the <code>WebSocket.SslConfiguration.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="p">...</span>
|
||||
<div class="highlight highlight-csharp"><pre>ws.SslConfiguration.ServerCertificateValidationCallback =
|
||||
(sender, certificate, chain, sslPolicyErrors) => {
|
||||
<span class="pl-c">// 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>
|
||||
<span class="pl-k">return</span> <span class="pl-c1">true</span>; <span class="pl-c">// If the server certificate is valid.</span>
|
||||
};</pre></div>
|
||||
|
||||
<p>If you set this property to nothing, the validation does nothing with the server certificate, and returns <code>true</code>.</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, like the following.</p>
|
||||
<p>As a <strong>WebSocket Server</strong>, you should create a new instance of the <code>WebSocketServer</code> or <code>HttpServer</code> class with some settings for secure connection, such as 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>
|
||||
<div class="highlight highlight-csharp"><pre><span class="pl-k">var</span> wssv = <span class="pl-s">new</span> WebSocketServer (<span class="pl-cn">4649</span>, <span class="pl-c1">true</span>);
|
||||
wssv.SslConfiguration.ServerCertificate =
|
||||
<span class="pl-s">new</span> X509Certificate2 (<span class="pl-s1"><span class="pl-pds">"</span>/path/to/cert.pfx<span class="pl-pds">"</span></span>, <span class="pl-s1"><span class="pl-pds">"</span>password for cert.pfx<span class="pl-pds">"</span></span>);</pre></div>
|
||||
|
||||
<h3>
|
||||
<a name="http-authentication" class="anchor" href="#http-authentication"><span class="octicon octicon-link"></span></a>HTTP Authentication</h3>
|
||||
<a id="http-authentication" class="anchor" href="#http-authentication" aria-hidden="true"><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>
|
||||
<p>As a <strong>WebSocket Client</strong>, you should set a pair of user name and password for the HTTP authentication, by 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="s">"nobita"</span><span class="p">,</span> <span class="s">"password"</span><span class="p">,</span> <span class="n">preAuth</span><span class="p">);</span>
|
||||
</pre></div>
|
||||
<div class="highlight highlight-csharp"><pre>ws.SetCredentials (<span class="pl-s1"><span class="pl-pds">"</span>nobita<span class="pl-pds">"</span></span>, <span class="pl-s1"><span class="pl-pds">"</span>password<span class="pl-pds">"</span></span>, preAuth);</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 (determined by the unauthorized response to the first connection request) authentication 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, like the following.</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, such as 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">id</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">id</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 aren't found.</span>
|
||||
<span class="p">};</span>
|
||||
</pre></div>
|
||||
<div class="highlight highlight-csharp"><pre>wssv.AuthenticationSchemes = AuthenticationSchemes.Basic;
|
||||
wssv.Realm = <span class="pl-s1"><span class="pl-pds">"</span>WebSocket Test<span class="pl-pds">"</span></span>;
|
||||
wssv.UserCredentialsFinder = id => {
|
||||
<span class="pl-k">var</span> name = id.Name;
|
||||
|
||||
<p>If you would like to provide the Digest authentication, you should set like the following.</p>
|
||||
<span class="pl-c">// Return user name, password, and roles.</span>
|
||||
<span class="pl-k">return</span> name == <span class="pl-s1"><span class="pl-pds">"</span>nobita<span class="pl-pds">"</span></span>
|
||||
? <span class="pl-s">new</span> NetworkCredential (name, <span class="pl-s1"><span class="pl-pds">"</span>password<span class="pl-pds">"</span></span>, <span class="pl-s1"><span class="pl-pds">"</span>gunfighter<span class="pl-pds">"</span></span>)
|
||||
: <span class="pl-c1">null</span>; <span class="pl-c">// If the user credentials aren't found.</span>
|
||||
};</pre></div>
|
||||
|
||||
<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>
|
||||
<p>If you would like to provide the Digest authentication, you should set such as the following.</p>
|
||||
|
||||
<div class="highlight highlight-csharp"><pre>wssv.AuthenticationSchemes = AuthenticationSchemes.Digest;</pre></div>
|
||||
|
||||
<h3>
|
||||
<a name="query-string-origin-header-and-cookies" class="anchor" href="#query-string-origin-header-and-cookies"><span class="octicon octicon-link"></span></a>Query String, Origin header and Cookies</h3>
|
||||
<a id="query-string-origin-header-and-cookies" class="anchor" href="#query-string-origin-header-and-cookies" aria-hidden="true"><span class="octicon octicon-link"></span></a>Query String, Origin header and Cookies</h3>
|
||||
|
||||
<p>As a <strong>WebSocket Client</strong>, if you would like to send the <strong>Query String</strong> with the WebSocket connection request to the server, you should create an instance of the <code>WebSocket</code> class with the WebSocket URL that includes the <a href="http://tools.ietf.org/html/rfc3986#section-3.4">Query</a> string parameters.</p>
|
||||
<p>As a <strong>WebSocket Client</strong>, if you would like to send the <strong>Query String</strong> with the WebSocket connection request to the server, you should create a new instance of the <code>WebSocket</code> class with the WebSocket URL that includes the <a href="http://tools.ietf.org/html/rfc3986#section-3.4">Query</a> string parameters.</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/?name=nobita"</span><span class="p">))</span> <span class="p">{</span>
|
||||
<span class="p">...</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
<div class="highlight highlight-csharp"><pre><span class="pl-ko">using</span> (<span class="pl-k">var</span> ws = <span class="pl-s">new</span> WebSocket (<span class="pl-s1"><span class="pl-pds">"</span>ws://example.com/?name=nobita<span class="pl-pds">"</span></span>)) {
|
||||
...
|
||||
}</pre></div>
|
||||
|
||||
<p>And if you would like to send the <strong>Origin header</strong> with the WebSocket connection request to the server, you should set the <code>WebSocket.Origin</code> property to an allowable value as the <a href="http://tools.ietf.org/html/rfc6454#section-7">Origin header</a> before connecting, like the following.</p>
|
||||
<p>And if you would like to send the <strong>Origin header</strong> with the WebSocket connection request to the server, you should set the <code>WebSocket.Origin</code> property to an allowable value as the <a href="http://tools.ietf.org/html/rfc6454#section-7">Origin header</a> before connecting, such as the following.</p>
|
||||
|
||||
<div class="highlight highlight-cs"><pre><span class="n">ws</span><span class="p">.</span><span class="n">Origin</span> <span class="p">=</span> <span class="s">"http://example.com"</span><span class="p">;</span>
|
||||
</pre></div>
|
||||
<div class="highlight highlight-csharp"><pre>ws.Origin = <span class="pl-s1"><span class="pl-pds">"</span>http://example.com<span class="pl-pds">"</span></span>;</pre></div>
|
||||
|
||||
<p>And if you would like to send the <strong>Cookies</strong> with the WebSocket connection request to the server, you should set any cookie using the <code>WebSocket.SetCookie (WebSocketSharp.Net.Cookie)</code> method before connecting, like the following.</p>
|
||||
<p>And also if you would like to send the <strong>Cookies</strong> with the WebSocket connection request to the server, you should set any cookie by using the <code>WebSocket.SetCookie (WebSocketSharp.Net.Cookie)</code> method before connecting, such as the following.</p>
|
||||
|
||||
<div class="highlight highlight-cs"><pre><span class="n">ws</span><span class="p">.</span><span class="n">SetCookie</span> <span class="p">(</span><span class="k">new</span> <span class="n">Cookie</span> <span class="p">(</span><span class="s">"name"</span><span class="p">,</span> <span class="s">"nobita"</span><span class="p">));</span>
|
||||
</pre></div>
|
||||
<div class="highlight highlight-csharp"><pre>ws.SetCookie (<span class="pl-s">new</span> Cookie (<span class="pl-s1"><span class="pl-pds">"</span>name<span class="pl-pds">"</span></span>, <span class="pl-s1"><span class="pl-pds">"</span>nobita<span class="pl-pds">"</span></span>));</pre></div>
|
||||
|
||||
<p>As a <strong>WebSocket Server</strong>, if you would like to get the <strong>Query String</strong> included in each WebSocket connection request, you should access the <code>WebSocketBehavior.Context.QueryString</code> property, like the following.</p>
|
||||
<p>As a <strong>WebSocket Server</strong>, if you would like to get the <strong>Query String</strong> included in each WebSocket connection request, you should access the <code>WebSocketBehavior.Context.QueryString</code> property, such as the following.</p>
|
||||
|
||||
<div class="highlight highlight-cs"><pre><span class="k">public</span> <span class="k">class</span> <span class="nc">Chat</span> <span class="p">:</span> <span class="n">WebSocketBehavior</span>
|
||||
<span class="p">{</span>
|
||||
<span class="k">private</span> <span class="kt">string</span> <span class="n">_name</span><span class="p">;</span>
|
||||
<span class="p">...</span>
|
||||
<div class="highlight highlight-csharp"><pre><span class="pl-s">public</span> <span class="pl-s">class</span> <span class="pl-entc">Chat</span> : <span class="pl-s">WebSocketBehavior</span>
|
||||
{
|
||||
<span class="pl-s">private</span> <span class="pl-s">string</span> _name;
|
||||
...
|
||||
|
||||
<span class="k">protected</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">OnOpen</span> <span class="p">()</span>
|
||||
<span class="p">{</span>
|
||||
<span class="n">_name</span> <span class="p">=</span> <span class="n">Context</span><span class="p">.</span><span class="n">QueryString</span><span class="p">[</span><span class="s">"name"</span><span class="p">];</span>
|
||||
<span class="p">}</span>
|
||||
<span class="pl-s">protected</span> <span class="pl-s">override</span> <span class="pl-s">void</span> <span class="pl-enf">OnOpen</span> ()
|
||||
{
|
||||
_name = Context.QueryString[<span class="pl-s1"><span class="pl-pds">"</span>name<span class="pl-pds">"</span></span>];
|
||||
}
|
||||
|
||||
<span class="p">...</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
...
|
||||
}</pre></div>
|
||||
|
||||
<p>And if you would like to validate the <strong>Origin header</strong>, <strong>Cookies</strong>, or both included in each WebSocket connection request, you should set each validation with your <code>WebSocketBehavior</code>, for example, using the <code>AddWebSocketService<TBehavior> (string, Func<TBehavior>)</code> method with initializing, like the following.</p>
|
||||
<p>And if you would like to validate the <strong>Origin header</strong>, <strong>Cookies</strong>, or both included in each WebSocket connection request, you should set each validation with your <code>WebSocketBehavior</code>, for example, by using the <code>AddWebSocketService<TBehavior> (string, Func<TBehavior>)</code> method with initializing, such as the following.</p>
|
||||
|
||||
<div class="highlight highlight-cs"><pre><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="p">()</span> <span class="p">=></span> <span class="k">new</span> <span class="n">Chat</span> <span class="p">()</span> <span class="p">{</span>
|
||||
<span class="n">OriginValidator</span> <span class="p">=</span> <span class="n">val</span> <span class="p">=></span> <span class="p">{</span>
|
||||
<span class="c1">// Check the value of the Origin header, and return true if valid.</span>
|
||||
<span class="n">Uri</span> <span class="n">origin</span><span class="p">;</span>
|
||||
<span class="k">return</span> <span class="p">!</span><span class="n">val</span><span class="p">.</span><span class="n">IsNullOrEmpty</span> <span class="p">()</span> <span class="p">&&</span>
|
||||
<span class="n">Uri</span><span class="p">.</span><span class="n">TryCreate</span> <span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="n">UriKind</span><span class="p">.</span><span class="n">Absolute</span><span class="p">,</span> <span class="k">out</span> <span class="n">origin</span><span class="p">)</span> <span class="p">&&</span>
|
||||
<span class="n">origin</span><span class="p">.</span><span class="n">Host</span> <span class="p">==</span> <span class="s">"example.com"</span><span class="p">;</span>
|
||||
<span class="p">},</span>
|
||||
<span class="n">CookiesValidator</span> <span class="p">=</span> <span class="p">(</span><span class="n">req</span><span class="p">,</span> <span class="n">res</span><span class="p">)</span> <span class="p">=></span> <span class="p">{</span>
|
||||
<span class="c1">// Check the Cookies in 'req', and set the Cookies to send to the client with 'res' if necessary.</span>
|
||||
<span class="k">foreach</span> <span class="p">(</span><span class="n">Cookie</span> <span class="n">cookie</span> <span class="k">in</span> <span class="n">req</span><span class="p">)</span> <span class="p">{</span>
|
||||
<span class="n">cookie</span><span class="p">.</span><span class="n">Expired</span> <span class="p">=</span> <span class="k">true</span><span class="p">;</span>
|
||||
<span class="n">res</span><span class="p">.</span><span class="n">Add</span> <span class="p">(</span><span class="n">cookie</span><span class="p">);</span>
|
||||
<span class="p">}</span>
|
||||
<div class="highlight highlight-csharp"><pre>wssv.AddWebSocketService<Chat> (
|
||||
<span class="pl-s1"><span class="pl-pds">"</span>/Chat<span class="pl-pds">"</span></span>,
|
||||
() => <span class="pl-s">new</span> Chat () {
|
||||
OriginValidator = val => {
|
||||
<span class="pl-c">// Check the value of the Origin header, and return true if valid.</span>
|
||||
Uri origin;
|
||||
<span class="pl-k">return</span> !val.IsNullOrEmpty () &&
|
||||
Uri.TryCreate (val, UriKind.Absolute, out origin) &&
|
||||
origin.Host == <span class="pl-s1"><span class="pl-pds">"</span>example.com<span class="pl-pds">"</span></span>;
|
||||
},
|
||||
CookiesValidator = (req, res) => {
|
||||
<span class="pl-c">// Check the Cookies in 'req', and set the Cookies to send to the client with 'res'</span>
|
||||
<span class="pl-c">// if necessary.</span>
|
||||
<span class="pl-k">foreach</span> (Cookie cookie <span class="pl-k">in</span> req) {
|
||||
cookie.Expired = <span class="pl-c1">true</span>;
|
||||
res.Add (cookie);
|
||||
}
|
||||
|
||||
<span class="k">return</span> <span class="k">true</span><span class="p">;</span> <span class="c1">// If valid.</span>
|
||||
<span class="p">}</span>
|
||||
<span class="p">});</span>
|
||||
</pre></div>
|
||||
<span class="pl-k">return</span> <span class="pl-c1">true</span>; <span class="pl-c">// If valid.</span>
|
||||
}
|
||||
});</pre></div>
|
||||
|
||||
<p>Also, if you would like to get each value of the Origin header and cookies, you should access each of the <code>WebSocketBehavior.Context.Origin</code> and <code>WebSocketBehavior.Context.CookieCollection</code> properties.</p>
|
||||
<p>And also if you would like to get each value of the Origin header and cookies, you should access each of the <code>WebSocketBehavior.Context.Origin</code> and <code>WebSocketBehavior.Context.CookieCollection</code> properties.</p>
|
||||
|
||||
<h3>
|
||||
<a name="connecting-through-the-http-proxy-server" class="anchor" href="#connecting-through-the-http-proxy-server"><span class="octicon octicon-link"></span></a>Connecting through the HTTP Proxy server</h3>
|
||||
<a id="connecting-through-the-http-proxy-server" class="anchor" href="#connecting-through-the-http-proxy-server" aria-hidden="true"><span class="octicon octicon-link"></span></a>Connecting through the HTTP Proxy server</h3>
|
||||
|
||||
<p>websocket-sharp supports to connect through the <strong>HTTP Proxy</strong> server.</p>
|
||||
|
||||
<p>If you would like to connect to a WebSocket server through the HTTP Proxy server, you should set the proxy server URL, and if necessary, a pair of user name and password for the proxy server authentication (Basic/Digest), using the <code>WebSocket.SetProxy (string, string, string)</code> method before connecting.</p>
|
||||
<p>If you would like to connect to a WebSocket server through the HTTP Proxy server, you should set the proxy server URL, and if necessary, a pair of user name and password for the proxy server authentication (Basic/Digest), by using the <code>WebSocket.SetProxy (string, string, string)</code> method before connecting.</p>
|
||||
|
||||
<div class="highlight highlight-cs"><pre><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="n">ws</span><span class="p">.</span><span class="n">SetProxy</span> <span class="p">(</span><span class="s">"http://localhost:3128"</span><span class="p">,</span> <span class="s">"nobita"</span><span class="p">,</span> <span class="s">"password"</span><span class="p">);</span>
|
||||
</pre></div>
|
||||
<div class="highlight highlight-csharp"><pre><span class="pl-k">var</span> ws = <span class="pl-s">new</span> WebSocket (<span class="pl-s1"><span class="pl-pds">"</span>ws://example.com<span class="pl-pds">"</span></span>);
|
||||
ws.SetProxy (<span class="pl-s1"><span class="pl-pds">"</span>http://localhost:3128<span class="pl-pds">"</span></span>, <span class="pl-s1"><span class="pl-pds">"</span>nobita<span class="pl-pds">"</span></span>, <span class="pl-s1"><span class="pl-pds">"</span>password<span class="pl-pds">"</span></span>);</pre></div>
|
||||
|
||||
<p>I tested this with the <a href="http://www.squid-cache.org">Squid</a>. And it's necessary to disable the following configuration option in <strong>squid.conf</strong> (e.g. <code>/etc/squid/squid.conf</code>).</p>
|
||||
|
||||
@ -612,57 +584,55 @@
|
||||
</code></pre>
|
||||
|
||||
<h3>
|
||||
<a name="logging" class="anchor" href="#logging"><span class="octicon octicon-link"></span></a>Logging</h3>
|
||||
<a id="logging" class="anchor" href="#logging" aria-hidden="true"><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>You can use 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>
|
||||
<div class="highlight highlight-csharp"><pre>ws.Log.Level = LogLevel.Debug;</pre></div>
|
||||
|
||||
<p>The above means a log with lower 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>
|
||||
<div class="highlight highlight-csharp"><pre>ws.Log.Debug (<span class="pl-s1"><span class="pl-pds">"</span>This is a debug message.<span class="pl-pds">"</span></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>
|
||||
<a id="examples" class="anchor" href="#examples" aria-hidden="true"><span class="octicon octicon-link"></span></a>Examples</h2>
|
||||
|
||||
<p>Examples using websocket-sharp.</p>
|
||||
|
||||
<h3>
|
||||
<a name="example" class="anchor" href="#example"><span class="octicon octicon-link"></span></a>Example</h3>
|
||||
<a id="example" class="anchor" href="#example" aria-hidden="true"><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>
|
||||
<a id="example1" class="anchor" href="#example1" aria-hidden="true"><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>
|
||||
<a id="example2" class="anchor" href="#example2" aria-hidden="true"><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>
|
||||
<a id="example3" class="anchor" href="#example3" aria-hidden="true"><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>Would 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>
|
||||
<a id="supported-websocket-specifications" class="anchor" href="#supported-websocket-specifications" aria-hidden="true"><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 it's based on the following WebSocket references:</p>
|
||||
|
||||
@ -680,7 +650,7 @@
|
||||
</ul>
|
||||
|
||||
<h2>
|
||||
<a name="license" class="anchor" href="#license"><span class="octicon octicon-link"></span></a>License</h2>
|
||||
<a id="license" class="anchor" href="#license" aria-hidden="true"><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>
|
||||
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user