672 lines
40 KiB
HTML
672 lines
40 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="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
|
|
<script src="javascripts/respond.js"></script>
|
|
<!--[if lt IE 9]>
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
<!--[if lt IE 8]>
|
|
<link rel="stylesheet" href="stylesheets/ie.css">
|
|
<![endif]-->
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
|
|
|
|
</head>
|
|
<body>
|
|
<div id="header">
|
|
<nav>
|
|
<li class="fork"><a href="https://github.com/sta/websocket-sharp">View On GitHub</a></li>
|
|
<li class="downloads"><a href="https://github.com/sta/websocket-sharp/zipball/master">ZIP</a></li>
|
|
<li class="downloads"><a href="https://github.com/sta/websocket-sharp/tarball/master">TAR</a></li>
|
|
<li class="title">DOWNLOADS</li>
|
|
</nav>
|
|
</div><!-- end header -->
|
|
|
|
<div class="wrapper">
|
|
|
|
<section>
|
|
<div id="title">
|
|
<h1>websocket-sharp</h1>
|
|
<p>A C# implementation of the WebSocket protocol client and server</p>
|
|
<hr>
|
|
<span class="credits left">Project maintained by <a href="https://github.com/sta">sta</a></span>
|
|
<span class="credits right">Hosted on GitHub Pages — Theme by <a href="https://twitter.com/michigangraham">mattgraham</a></span>
|
|
</div>
|
|
|
|
<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>
|
|
|
|
<ul>
|
|
<li><strong><a href="#supported-websocket-specifications">RFC 6455</a></strong></li>
|
|
<li>
|
|
<strong><a href="#websocket-client">WebSocket Client</a></strong> and <strong><a href="#websocket-server">Server</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><strong><a href="#query-string-origin-header-and-cookies">Query String, Origin header and Cookies</a></strong></li>
|
|
<li><strong><a href="#connecting-through-the-http-proxy-server">Connecting through the HTTP Proxy server</a></strong></li>
|
|
<li>.NET <strong>3.5</strong> or later (includes compatible)</li>
|
|
</ul>
|
|
|
|
<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 <strong>websocket-sharp project</strong> with any of the build configurations (e.g. <code>Debug</code>) in MonoDevelop.</p>
|
|
|
|
<h2>
|
|
<a id="install" class="anchor" href="#install" aria-hidden="true"><span class="octicon octicon-link"></span></a>Install</h2>
|
|
|
|
<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 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 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 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>
|
|
|
|
<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 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 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>
|
|
|
|
<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 works with <strong>Unity Free</strong>, but there are some limitations:</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<strong><a href="http://docs.unity3d.com/Manual/SecuritySandbox.html">Security Sandbox of the Webplayer</a></strong> (server doesn't work in the webplayer)</li>
|
|
<li>
|
|
<strong><a href="http://unity3d.com/unity/licenses">.NET Socket Support for iOS/Android</a></strong> (requires iOS/Android Pro)</li>
|
|
<li><strong>.NET API 2.0 compatibility level for iOS/Android</strong></li>
|
|
</ul>
|
|
|
|
<p>Using <strong>.NET API 2.0 compatibility level for iOS/Android</strong> requires to fix lack of some features for later than .NET 2.0, such as the <code>System.Func<...></code> delegates (so i've fixed it in the asset package).</p>
|
|
|
|
<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 id="usage" class="anchor" href="#usage" aria-hidden="true"><span class="octicon octicon-link"></span></a>Usage</h2>
|
|
|
|
<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-csharp"><pre><span class="pl-k">using</span> System;
|
|
<span class="pl-k">using</span> WebSocketSharp;
|
|
|
|
<span class="pl-k">namespace</span> <span class="pl-en">Example</span>
|
|
{
|
|
<span class="pl-s">public</span> <span class="pl-s">class</span> <span class="pl-en">Program</span>
|
|
{
|
|
<span class="pl-s">public</span> <span class="pl-s">static</span> <span class="pl-st">void</span> <span class="pl-en">Main</span> (<span class="pl-st">string[]</span> <span class="pl-vpf">args</span>)
|
|
{
|
|
<span class="pl-k">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);
|
|
|
|
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 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-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 id="step-2" class="anchor" href="#step-2" aria-hidden="true"><span class="octicon octicon-link"></span></a>Step 2</h4>
|
|
|
|
<p>Creating a new instance of the <code>WebSocket</code> class with the WebSocket URL to connect.</p>
|
|
|
|
<div class="highlight highlight-csharp"><pre><span class="pl-k">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 will be closed with close status <code>1001</code> (going away) when the control leaves the <code>using</code> block.</p>
|
|
|
|
<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 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-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 need to use it.</p>
|
|
|
|
<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-csharp"><pre>ws.OnMessage += (sender, e) => {
|
|
...
|
|
};</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 message. So by checking it, you can determine which item you should use.</p>
|
|
|
|
<p>If it returns <code>Opcode.Text</code>, you should use <code>e.Data</code> property that returns a <code>string</code> (represents the <strong>Text</strong> message).</p>
|
|
|
|
<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-csharp"><pre><span class="pl-k">if</span> (e.Type == Opcode.Text) {
|
|
<span class="pl-c">// Do something with e.Data.</span>
|
|
...
|
|
|
|
<span class="pl-k">return</span>;
|
|
}
|
|
|
|
<span class="pl-k">if</span> (e.Type == Opcode.Binary) {
|
|
<span class="pl-c">// Do something with e.RawData.</span>
|
|
...
|
|
|
|
<span class="pl-k">return</span>;
|
|
}</pre></div>
|
|
|
|
<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-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.</p>
|
|
|
|
<p>If the error is due to an exception, <code>e.Exception</code> property returns a <code>System.Exception</code> instance that caused the error.</p>
|
|
|
|
<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-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.</p>
|
|
|
|
<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-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 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-csharp"><pre>ws.Send (data);</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>, or <code>WebSocket.Send (System.IO.FileInfo)</code> method 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-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 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-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>
|
|
|
|
<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 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-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="pl-k">namespace</span> <span class="pl-en">Example</span>
|
|
{
|
|
<span class="pl-s">public</span> <span class="pl-s">class</span> <span class="pl-en">Laputa</span> : <span class="pl-st">WebSocketBehavior</span>
|
|
{
|
|
<span class="pl-s">protected</span> <span class="pl-s">override</span> <span class="pl-st">void</span> <span class="pl-en">OnMessage</span> (<span class="pl-st">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>;
|
|
|
|
Send (msg);
|
|
}
|
|
}
|
|
|
|
<span class="pl-s">public</span> <span class="pl-s">class</span> <span class="pl-en">Program</span>
|
|
{
|
|
<span class="pl-s">public</span> <span class="pl-s">static</span> <span class="pl-st">void</span> <span class="pl-en">Main</span> (<span class="pl-st">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 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-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 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-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="pl-s">public</span> <span class="pl-s">class</span> <span class="pl-en">Echo</span> : <span class="pl-st">WebSocketBehavior</span>
|
|
{
|
|
<span class="pl-s">protected</span> <span class="pl-s">override</span> <span class="pl-st">void</span> <span class="pl-en">OnMessage</span> (<span class="pl-st">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-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="pl-s">public</span> <span class="pl-s">class</span> <span class="pl-en">Chat</span> : <span class="pl-st">WebSocketBehavior</span>
|
|
{
|
|
<span class="pl-s">private</span> <span class="pl-st">string</span> _suffix;
|
|
|
|
<span class="pl-s">public</span> <span class="pl-en">Chat</span> ()
|
|
: <span class="pl-c1">this</span> (null)
|
|
{
|
|
}
|
|
|
|
<span class="pl-s">public</span> <span class="pl-en">Chat</span> (<span class="pl-st">string</span> <span class="pl-vpf">suffix</span>)
|
|
{
|
|
_suffix = suffix ?? String.Empty;
|
|
}
|
|
|
|
<span class="pl-s">protected</span> <span class="pl-s">override</span> <span class="pl-st">void</span> <span class="pl-en">OnMessage</span> (<span class="pl-st">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>
|
|
|
|
<p>If you override the <code>WebSocketBehavior.OnMessage (MessageEventArgs)</code> method, it's called when the <code>WebSocket</code> used in the current session in the service receives a message.</p>
|
|
|
|
<p>And if you override the <code>WebSocketBehavior.OnOpen ()</code>, <code>WebSocketBehavior.OnError (ErrorEventArgs)</code>, and <code>WebSocketBehavior.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> events) occurs.</p>
|
|
|
|
<p>The <code>WebSocketBehavior.Send</code> method sends a data to the client on the current session in the service.</p>
|
|
|
|
<p>If you would like to access the sessions in the service, you should use the <code>WebSocketBehavior.Sessions</code> property (returns a <code>WebSocketSharp.Server.WebSocketSessionManager</code>).</p>
|
|
|
|
<p>The <code>WebSocketBehavior.Sessions.Broadcast</code> method broadcasts a data to every client in the service.</p>
|
|
|
|
<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 a new instance of the <code>WebSocketServer</code> class.</p>
|
|
|
|
<div class="highlight highlight-csharp"><pre><span class="pl-k">var</span> wssv = <span class="pl-s">new</span> WebSocketServer (<span class="pl-c1">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, 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>
|
|
|
|
<p>And also the type of <code>TBehavior</code> must inherit the <code>WebSocketBehavior</code> class.</p>
|
|
|
|
<p>So you can use the classes created in <strong>Step 2</strong> to add the service.</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 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-csharp"><pre>wssv.Start ();</pre></div>
|
|
|
|
<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-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 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, by using the <code>HttpServer.AddWebSocketService<TBehaviorWithNew> (string)</code> or <code>HttpServer.AddWebSocketService<TBehavior> (string, Func<TBehavior>)</code> method.</p>
|
|
|
|
<div class="highlight highlight-csharp"><pre><span class="pl-k">var</span> httpsv = <span class="pl-s">new</span> HttpServer (<span class="pl-c1">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 id="websocket-extensions" class="anchor" href="#websocket-extensions" aria-hidden="true"><span class="octicon octicon-link"></span></a>WebSocket Extensions</h3>
|
|
|
|
<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 such as the following.</p>
|
|
|
|
<div class="highlight highlight-csharp"><pre>ws.Compression = CompressionMethod.Deflate;</pre></div>
|
|
|
|
<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 will return the same header. And when your client receives it, this extension will be available.</p>
|
|
|
|
<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</strong> with <strong>SSL/TLS</strong>.</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-csharp"><pre><span class="pl-k">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 use the custom validation for the server certificate, you should set the <code>WebSocket.SslConfiguration.ServerCertificateValidationCallback</code> property.</p>
|
|
|
|
<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="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 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-csharp"><pre><span class="pl-k">var</span> wssv = <span class="pl-s">new</span> WebSocketServer (<span class="pl-c1">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 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, by using the <code>WebSocket.SetCredentials (string, string, bool)</code> method before connecting.</p>
|
|
|
|
<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, such as the following.</p>
|
|
|
|
<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;
|
|
|
|
<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>
|
|
|
|
<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 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 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-csharp"><pre><span class="pl-k">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, such as the following.</p>
|
|
|
|
<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 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-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, such as the following.</p>
|
|
|
|
<div class="highlight highlight-csharp"><pre><span class="pl-s">public</span> <span class="pl-s">class</span> <span class="pl-en">Chat</span> : <span class="pl-st">WebSocketBehavior</span>
|
|
{
|
|
<span class="pl-s">private</span> <span class="pl-st">string</span> _name;
|
|
...
|
|
|
|
<span class="pl-s">protected</span> <span class="pl-s">override</span> <span class="pl-st">void</span> <span class="pl-en">OnOpen</span> ()
|
|
{
|
|
_name = Context.QueryString[<span class="pl-s1"><span class="pl-pds">"</span>name<span class="pl-pds">"</span></span>];
|
|
}
|
|
|
|
...
|
|
}</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, by using the <code>AddWebSocketService<TBehavior> (string, Func<TBehavior>)</code> method with initializing, such as the following.</p>
|
|
|
|
<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="pl-k">return</span> <span class="pl-c1">true</span>; <span class="pl-c">// If valid.</span>
|
|
}
|
|
});</pre></div>
|
|
|
|
<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 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), by using the <code>WebSocket.SetProxy (string, string, string)</code> method before connecting.</p>
|
|
|
|
<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>
|
|
|
|
<pre><code># Deny CONNECT to other than SSL ports
|
|
#http_access deny CONNECT !SSL_ports
|
|
</code></pre>
|
|
|
|
<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 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-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-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 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 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 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 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 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 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>
|
|
|
|
<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 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>
|
|
|
|
</div>
|
|
<!--[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> |