2014-02-11 13:40:36 +08:00

2010-10-19 10:27:39 +08:00
2013-12-03 13:24:06 +08:00
## Welcome to websocket-sharp! ##
2014-02-26 16:02:26 +08:00
**websocket-sharp** supports:
2013-11-30 00:41:30 +08:00
2013-12-03 13:24:06 +08:00
- **[WebSocket Client](#websocket-client)** and ** [Server ](#websocket-server )**
2013-11-30 00:57:02 +08:00
- **[RFC 6455](#supported-websocket-specifications)**
- **[Per-message Compression](#per-message-compression)** extension
2013-11-30 00:41:30 +08:00
- **[Secure Connection](#secure-connection)**
2014-01-18 16:05:57 +08:00
- **[HTTP Authentication](#http-authentication)**
2013-12-01 13:19:04 +08:00
- .NET **3.5** or later (includes compatible)
2010-10-19 10:27:39 +08:00
2013-11-22 15:15:44 +08:00
## Branches ##
2013-11-30 00:41:30 +08:00
- **[master]** for production releases.
- **[hybi-00]** for older [draft-ietf-hybi-thewebsocketprotocol-00]. No longer maintained.
- **[draft75]** for even more old [draft-hixie-thewebsocketprotocol-75]. No longer maintained.
2013-11-22 15:15:44 +08:00
2013-11-17 16:03:52 +08:00
## Build ##
2014-01-19 17:06:35 +08:00
websocket-sharp is built as a single assembly, **websocket-sharp.dll** .
2013-11-17 16:03:52 +08:00
2014-04-04 15:59:38 +08:00
websocket-sharp is developed with ** [MonoDevelop]**. So the simple way to build is to open **websocket-sharp.sln** and run build for the **websocket-sharp project** with any of the build configurations (e.g. Debug) in the MonoDevelop.
2013-11-17 16:03:52 +08:00
2013-11-17 17:10:21 +08:00
## Install ##
### Self Build ###
2014-01-01 21:09:41 +08:00
You should add **websocket-sharp.dll** (e.g. /path/to/websocket-sharp/bin/Debug/websocket-sharp.dll) built yourself to the library references of your project.
2013-11-17 17:10:21 +08:00
2014-01-27 16:05:28 +08:00
If you would like to use that websocket-sharp.dll in your ** [Unity]** project, you should add that dll to any folder of your project (e.g. Assets/Plugins) in the **Unity Editor** .
2013-11-18 03:59:29 +08:00
2013-11-17 17:10:21 +08:00
### NuGet Gallery ###
2014-01-19 17:06:35 +08:00
websocket-sharp is available on the ** [NuGet Gallery]**, as still a **prerelease** version.
2013-11-17 17:10:21 +08:00
- **[NuGet Gallery: websocket-sharp]**
2013-11-22 15:15:44 +08:00
You can add websocket-sharp to your project using the **NuGet Package Manager** , the following command in the **Package Manager Console** .
2013-11-17 17:10:21 +08:00
2013-11-22 20:11:19 +08:00
PM> Install-Package WebSocketSharp -Pre
2013-11-17 17:10:21 +08:00
### Unity Asset Store ###
2014-01-19 17:06:35 +08:00
websocket-sharp is available on the **Unity Asset Store** .
2013-11-17 17:10:21 +08:00
2014-01-19 17:06:35 +08:00
- **[WebSocket-Sharp for Unity]**
2013-11-17 17:10:21 +08:00
2013-12-01 13:19:04 +08:00
It's priced at **US$15** . I think your $15 makes this project more better and accelerated, **Thank you!**
2013-11-17 17:10:21 +08:00
2010-10-19 10:27:39 +08:00
## Usage ##
2013-07-01 11:18:26 +08:00
### WebSocket Client ###
2012-08-04 14:51:31 +08:00
2013-04-12 23:41:04 +08:00
```cs
using System;
using WebSocketSharp;
2013-07-30 15:32:21 +08:00
namespace Example
{
public class Program
{
public static void Main (string [] args)
2013-04-12 23:41:04 +08:00
{
2014-01-01 21:09:41 +08:00
using (var ws = new WebSocket ("ws://dragonsnest.far/Laputa")) {
2014-01-25 16:35:47 +08:00
ws.OnMessage += (sender, e) =>
2013-11-15 21:11:21 +08:00
Console.WriteLine ("Laputa says: " + e.Data);
2013-04-12 23:41:04 +08:00
2013-07-30 15:32:21 +08:00
ws.Connect ();
ws.Send ("BALUS");
Console.ReadKey (true);
2013-04-12 23:41:04 +08:00
}
}
}
}
```
2012-08-04 14:51:31 +08:00
#### Step 1 ####
2010-10-19 10:27:39 +08:00
2013-01-28 15:22:24 +08:00
Required namespace.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
using WebSocketSharp;
```
2012-07-31 09:36:52 +08:00
2013-01-15 14:29:05 +08:00
The `WebSocket` class exists in the `WebSocketSharp` namespace.
2012-07-31 09:36:52 +08:00
2012-08-04 14:51:31 +08:00
#### Step 2 ####
2012-07-31 09:36:52 +08:00
2014-01-20 16:04:20 +08:00
Creating an instance of the `WebSocket` class with the WebSocket URL to connect.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
2014-01-01 21:09:41 +08:00
using (var ws = new WebSocket ("ws://example.com")) {
2012-08-08 10:32:19 +08:00
...
}
```
2012-07-31 09:36:52 +08:00
2014-01-20 18:33:21 +08:00
The `WebSocket` class inherits the `System.IDisposable` interface, so you can use the `using` statement.
2012-07-31 09:36:52 +08:00
2012-08-04 14:51:31 +08:00
#### Step 3 ####
2012-07-31 09:36:52 +08:00
2012-10-12 13:53:29 +08:00
Setting the `WebSocket` events.
2012-07-31 09:36:52 +08:00
2013-07-24 22:05:33 +08:00
##### WebSocket.OnOpen Event #####
2012-07-31 09:36:52 +08:00
2013-07-15 19:42:55 +08:00
A `WebSocket.OnOpen` event occurs when the WebSocket connection has been established.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
2014-01-01 21:09:41 +08:00
ws.OnOpen += (sender, e) => {
2012-08-08 10:32:19 +08:00
...
};
```
2012-07-31 09:36:52 +08:00
2014-01-20 18:33:21 +08:00
`e` has passed as the `System.EventArgs.Empty` , so you don't use `e` .
2012-07-31 09:36:52 +08:00
2013-07-24 22:05:33 +08:00
##### WebSocket.OnMessage Event #####
2012-07-31 09:36:52 +08:00
2014-02-26 16:02:26 +08:00
A `WebSocket.OnMessage` event occurs when the `WebSocket` receives a message.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
2014-01-01 21:09:41 +08:00
ws.OnMessage += (sender, e) => {
2012-08-08 10:32:19 +08:00
...
};
```
2012-07-31 09:36:52 +08:00
2014-01-20 16:04:20 +08:00
`e` has passed as a `WebSocketSharp.MessageEventArgs` .
2013-07-19 16:29:58 +08:00
2014-03-06 16:07:30 +08:00
`e.Type` property returns either `WebSocketSharp.Opcode.Text` or `WebSocketSharp.Opcode.Binary` that represents the type of the received message. So by checking it, you determine which item you should use.
2013-07-19 16:29:58 +08:00
2014-03-06 16:07:30 +08:00
If `e.Type` is `Opcode.Text` , you should use `e.Data` property (returns a `string` ) that represents the received **Text** message.
2014-01-20 16:04:20 +08:00
2014-03-06 16:07:30 +08:00
Or if `e.Type` is `Opcode.Binary` , you should use `e.RawData` property (returns a `byte []` ) that represents the received **Binary** message.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
2014-03-06 16:07:30 +08:00
if (e.Type == Opcode.Text) {
2013-07-15 19:42:55 +08:00
// Do something with e.Data
return;
}
2014-03-06 16:07:30 +08:00
if (e.Type == Opcode.Binary) {
2013-07-15 19:42:55 +08:00
// Do something with e.RawData
return;
2012-08-08 10:36:17 +08:00
}
2012-08-08 10:32:19 +08:00
```
2012-07-31 09:36:52 +08:00
2013-07-24 22:05:33 +08:00
##### WebSocket.OnError Event #####
2012-07-31 09:36:52 +08:00
2013-07-15 19:42:55 +08:00
A `WebSocket.OnError` event occurs when the `WebSocket` gets an error.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
2014-01-01 21:09:41 +08:00
ws.OnError += (sender, e) => {
2012-08-08 10:32:19 +08:00
...
};
```
2013-11-22 20:11:19 +08:00
2014-01-20 16:04:20 +08:00
`e` has passed as a `WebSocketSharp.ErrorEventArgs` .
2014-01-24 02:02:51 +08:00
`e.Message` property returns a `string` that represents the error message. So you should use it to get the error message.
2012-07-31 09:36:52 +08:00
2013-07-24 22:05:33 +08:00
##### WebSocket.OnClose Event #####
2012-07-31 09:36:52 +08:00
2013-07-15 19:42:55 +08:00
A `WebSocket.OnClose` event occurs when the WebSocket connection has been closed.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
2014-01-01 21:09:41 +08:00
ws.OnClose += (sender, e) => {
2012-08-08 10:32:19 +08:00
...
};
```
2012-07-31 09:36:52 +08:00
2014-01-20 16:04:20 +08:00
`e` has passed as a `WebSocketSharp.CloseEventArgs` .
2014-02-26 16:02:26 +08:00
`e.Code` property returns a `ushort` that represents the status code indicating the reason for closure, and `e.Reason` property returns a `string` that represents the reason for closure. So you should use them to get the reason for closure.
2012-07-31 09:36:52 +08:00
2012-08-04 14:51:31 +08:00
#### Step 4 ####
2012-07-31 09:36:52 +08:00
2012-10-12 13:53:29 +08:00
Connecting to the WebSocket server.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
2013-07-30 15:32:21 +08:00
ws.Connect ();
2012-08-08 10:32:19 +08:00
```
2012-07-31 09:36:52 +08:00
2014-01-27 16:05:28 +08:00
If you would like to connect to the server asynchronously, you should use the `WebSocket.ConnectAsync ()` method.
2014-01-17 19:42:43 +08:00
2012-08-04 14:51:31 +08:00
#### Step 5 ####
2012-07-31 09:36:52 +08:00
2014-01-17 19:42:43 +08:00
Sending a data to the WebSocket server.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
2013-07-30 15:32:21 +08:00
ws.Send (data);
2012-08-08 10:32:19 +08:00
```
2012-07-31 09:36:52 +08:00
2014-01-21 13:51:55 +08:00
The `WebSocket.Send` method is overloaded.
2012-07-31 09:36:52 +08:00
2014-04-04 15:59:38 +08:00
You can use the `WebSocket.Send (string)` , `WebSocket.Send (byte [])` , or `WebSocket.Send (System.IO.FileInfo)` method to send a data.
2013-10-15 23:18:05 +08:00
2014-01-27 16:05:28 +08:00
If you would like to send a data asynchronously, you should use the `WebSocket.SendAsync` method.
2014-01-21 14:20:06 +08:00
```cs
ws.SendAsync (data, completed);
```
2013-10-15 23:18:05 +08:00
2014-01-27 16:05:28 +08:00
And if you would like to do something when the send is complete, you should set `completed` to any `Action<bool>` .
2012-07-31 09:36:52 +08:00
2012-08-04 14:51:31 +08:00
#### Step 6 ####
2012-07-31 09:36:52 +08:00
2012-10-12 13:53:29 +08:00
Closing the WebSocket connection.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
2014-01-21 14:01:50 +08:00
ws.Close (code, reason);
2012-08-08 10:32:19 +08:00
```
2012-07-31 09:36:52 +08:00
2014-01-27 16:05:28 +08:00
If you would like to close the connection explicitly, you should use the `WebSocket.Close` method.
2012-07-31 09:36:52 +08:00
2013-11-22 20:11:19 +08:00
The `WebSocket.Close` method is overloaded.
2013-10-15 23:18:05 +08:00
2014-01-21 14:06:25 +08:00
You can use the `WebSocket.Close ()` , `WebSocket.Close (ushort)` , `WebSocket.Close (WebSocketSharp.CloseStatusCode)` , `WebSocket.Close (ushort, string)` , or `WebSocket.Close (WebSocketSharp.CloseStatusCode, string)` method to close the connection.
2012-10-16 19:31:50 +08:00
2014-01-27 16:05:28 +08:00
If you would like to close the connection asynchronously, you should use the `WebSocket.CloseAsync` method.
2014-01-17 19:42:43 +08:00
2013-07-01 11:18:26 +08:00
### WebSocket Server ###
2012-08-04 14:51:31 +08:00
2013-04-12 23:41:04 +08:00
```cs
using System;
using WebSocketSharp;
using WebSocketSharp.Server;
2013-07-30 15:32:21 +08:00
namespace Example
{
2013-04-12 23:41:04 +08:00
public class Laputa : WebSocketService
{
2013-07-30 15:32:21 +08:00
protected override void OnMessage (MessageEventArgs e)
2013-04-12 23:41:04 +08:00
{
2013-11-18 02:17:18 +08:00
var msg = e.Data == "BALUS"
2014-04-04 15:59:38 +08:00
? "I've been balused already..."
: "I'm not available now.";
2013-11-05 20:55:16 +08:00
2013-07-30 15:32:21 +08:00
Send (msg);
2013-04-12 23:41:04 +08:00
}
}
2013-07-30 15:32:21 +08:00
public class Program
{
public static void Main (string [] args)
2013-04-12 23:41:04 +08:00
{
2013-10-02 13:39:56 +08:00
var wssv = new WebSocketServer ("ws://dragonsnest.far");
wssv.AddWebSocketService< Laputa > ("/Laputa");
2013-07-30 15:32:21 +08:00
wssv.Start ();
Console.ReadKey (true);
wssv.Stop ();
2013-04-12 23:41:04 +08:00
}
}
}
```
2012-08-04 14:51:31 +08:00
#### Step 1 ####
2012-08-06 22:04:57 +08:00
Required namespace.
2012-08-04 14:51:31 +08:00
2012-08-08 10:32:19 +08:00
```cs
using WebSocketSharp.Server;
```
2012-08-04 14:51:31 +08:00
2014-01-20 19:38:26 +08:00
The `WebSocketServer` and `WebSocketService` classes exist in the `WebSocketSharp.Server` namespace.
2012-08-04 14:51:31 +08:00
#### Step 2 ####
2013-10-02 13:39:56 +08:00
Creating the class that inherits the `WebSocketService` class.
2012-08-04 14:51:31 +08:00
2014-01-27 16:05:28 +08:00
For example, if you would like to provide an echo service,
2012-08-04 14:51:31 +08:00
2012-08-08 10:20:57 +08:00
```cs
using System;
using WebSocketSharp;
using WebSocketSharp.Server;
public class Echo : WebSocketService
{
2013-07-30 15:32:21 +08:00
protected override void OnMessage (MessageEventArgs e)
2012-08-08 10:20:57 +08:00
{
2013-07-30 15:32:21 +08:00
Send (e.Data);
2012-08-08 10:20:57 +08:00
}
}
```
2012-08-04 14:51:31 +08:00
2014-01-27 16:05:28 +08:00
And if you would like to provide a chat service,
2012-08-04 14:51:31 +08:00
2012-08-08 10:32:19 +08:00
```cs
using System;
using WebSocketSharp;
using WebSocketSharp.Server;
public class Chat : WebSocketService
{
2013-10-02 13:39:56 +08:00
private string _suffix;
public Chat ()
2013-12-03 14:45:14 +08:00
: this (null)
2013-10-02 13:39:56 +08:00
{
}
public Chat (string suffix)
{
2013-12-03 14:45:14 +08:00
_suffix = suffix ?? String.Empty;
2013-10-02 13:39:56 +08:00
}
2013-07-30 15:32:21 +08:00
protected override void OnMessage (MessageEventArgs e)
2012-08-08 10:32:19 +08:00
{
2013-10-02 13:39:56 +08:00
Sessions.Broadcast (e.Data + _suffix);
2012-08-08 10:32:19 +08:00
}
}
```
2012-08-04 14:51:31 +08:00
2014-02-26 16:02:26 +08:00
If you override the `WebSocketService.OnMessage (MessageEventArgs)` method, it's called when the `OnMessage` event of the `WebSocket` used by the current session in the WebSocket service occurs.
2012-08-13 11:31:11 +08:00
2014-03-06 16:42:12 +08:00
And if you override the `WebSocketService.OnOpen ()` , `WebSocketService.OnError (ErrorEventArgs)` , and `WebSocketService.OnClose (CloseEventArgs)` methods, each of them is called when each event of the `WebSocket` (the `OnOpen` , `OnError` , and `OnClose` events) occurs.
2012-08-13 11:31:11 +08:00
2014-02-04 16:41:36 +08:00
The `WebSocketService.Send` method sends a data to the client on the current session in the WebSocket service.
2013-11-22 15:15:44 +08:00
2014-02-04 16:41:36 +08:00
If you would like to access the sessions in the WebSocket service, you should use the `WebSocketService.Sessions` property (returns a `WebSocketSharp.Server.WebSocketSessionManager` ).
2013-11-22 15:15:44 +08:00
2014-02-26 16:02:26 +08:00
The `WebSocketService.Sessions.Broadcast` method broadcasts a data to every client in the WebSocket service.
2013-11-22 15:15:44 +08:00
2012-08-06 13:34:39 +08:00
#### Step 3 ####
2012-08-04 14:51:31 +08:00
2013-10-02 13:39:56 +08:00
Creating an instance of the `WebSocketServer` class.
2012-09-24 14:01:25 +08:00
```cs
2013-07-30 15:32:21 +08:00
var wssv = new WebSocketServer (4649);
wssv.AddWebSocketService< Echo > ("/Echo");
2013-10-02 14:45:24 +08:00
wssv.AddWebSocketService< Chat > ("/Chat");
wssv.AddWebSocketService< Chat > ("/ChatWithNiceBoat", () => new Chat (" Nice boat."));
2012-09-24 14:01:25 +08:00
```
2014-02-26 16:02:26 +08:00
You can add any WebSocket service to your `WebSocketServer` with the specified path to the service, using the `WebSocketServer.AddWebSocketService<TWithNew> (string)` or `WebSocketServer.AddWebSocketService<T> (string, Func<T>)` method.
2013-10-02 13:39:56 +08:00
The type of `TWithNew` must inherit the `WebSocketService` class and must have a public parameterless constructor.
2014-01-21 02:04:13 +08:00
The type of `T` must inherit the `WebSocketService` class.
2012-09-24 14:01:25 +08:00
2014-01-21 02:04:13 +08:00
So you can use the classes created in **Step 2** to add the WebSocket service.
2012-08-04 14:51:31 +08:00
2014-01-21 02:04:13 +08:00
If you create an instance of the `WebSocketServer` class without a port number, the `WebSocketServer` set the port number to **80** automatically. So it's necessary to run with root permission.
2012-08-04 14:51:31 +08:00
2012-08-06 13:34:39 +08:00
$ sudo mono example2.exe
2012-08-04 14:51:31 +08:00
2012-08-06 13:34:39 +08:00
#### Step 4 ####
2012-08-04 14:51:31 +08:00
2014-01-21 02:04:13 +08:00
Starting the WebSocket server.
2012-08-04 14:51:31 +08:00
2012-08-08 10:32:19 +08:00
```cs
2013-07-30 15:32:21 +08:00
wssv.Start ();
2012-08-08 10:32:19 +08:00
```
2012-08-04 14:51:31 +08:00
2013-08-29 16:01:37 +08:00
#### Step 5 ####
2012-08-04 14:51:31 +08:00
2014-01-21 02:04:13 +08:00
Stopping the WebSocket server.
2012-08-04 14:51:31 +08:00
2012-08-08 10:32:19 +08:00
```cs
2014-01-21 13:22:12 +08:00
wssv.Stop (code, reason);
2012-08-08 10:32:19 +08:00
```
2012-08-04 14:51:31 +08:00
2014-01-21 02:04:13 +08:00
The `WebSocketServer.Stop` method is overloaded.
2014-01-21 13:22:12 +08:00
You can use the `WebSocketServer.Stop ()` , `WebSocketServer.Stop (ushort, string)` , or `WebSocketServer.Stop (WebSocketSharp.CloseStatusCode, string)` method to stop the server.
2014-01-21 02:04:13 +08:00
2013-07-01 11:18:26 +08:00
### HTTP Server with the WebSocket ###
2012-09-10 00:36:22 +08:00
2014-02-04 16:32:03 +08:00
I modified the `System.Net.HttpListener` , `System.Net.HttpListenerContext` , and some other classes of ** [Mono]** to create the HTTP server that allows to accept the WebSocket connection requests.
2012-09-10 00:36:22 +08:00
2014-01-21 16:41:00 +08:00
So websocket-sharp provides the `WebSocketSharp.Server.HttpServer` class.
2014-02-26 16:02:26 +08:00
You can add any WebSocket service to your `HttpServer` with the specified path to the service, using the `HttpServer.AddWebSocketService<TWithNew> (string)` or `HttpServer.AddWebSocketService<T> (string, Func<T>)` method.
2012-09-10 00:36:22 +08:00
```cs
2013-07-30 15:32:21 +08:00
var httpsv = new HttpServer (4649);
2013-10-02 13:39:56 +08:00
httpsv.AddWebSocketService< Echo > ("/Echo");
2013-10-02 14:45:24 +08:00
httpsv.AddWebSocketService< Chat > ("/Chat");
httpsv.AddWebSocketService< Chat > ("/ChatWithNiceBoat", () => new Chat (" Nice boat."));
2012-09-10 00:36:22 +08:00
```
2013-07-01 11:18:26 +08:00
For more information, could you see ** [Example3]**?
2013-11-05 20:55:16 +08:00
### WebSocket Extensions ###
#### Per-message Compression ####
2014-01-21 19:25:17 +08:00
websocket-sharp supports the ** [Per-message Compression][compression]** extension. (But it doesn't support with the [extension parameters].)
2013-11-05 20:55:16 +08:00
2014-01-27 16:05:28 +08:00
If you would like to enable this extension as a WebSocket client, you should set like the following.
2013-11-05 20:55:16 +08:00
```cs
2014-03-04 16:02:41 +08:00
ws.Compression = CompressionMethod.Deflate;
2013-11-05 20:55:16 +08:00
```
2014-01-21 20:00:33 +08:00
And then your client sends the following header with the connection request to the server.
2013-11-05 20:55:16 +08:00
2013-11-22 20:11:19 +08:00
Sec-WebSocket-Extensions: permessage-deflate
2013-11-05 20:55:16 +08:00
2014-01-21 19:25:17 +08:00
If the server supports this extension, it returns the same header. And when your client receives that header, it enables this extension.
2013-11-05 20:55:16 +08:00
2013-07-19 16:29:58 +08:00
### Secure Connection ###
2014-01-21 19:50:14 +08:00
websocket-sharp supports the **Secure Connection (SSL)** .
As a **WebSocket Client** , you should create an instance of the `WebSocket` class with the **wss** scheme WebSocket URL to connect.
2013-07-19 16:29:58 +08:00
```cs
2014-01-01 21:09:41 +08:00
using (var ws = new WebSocket ("wss://example.com")) {
2013-07-19 16:29:58 +08:00
...
}
```
2014-02-04 16:32:03 +08:00
And if you would like to set the custom validation for the server certificate, you should set the `WebSocket.ServerCertificateValidationCallback` property.
2013-07-19 16:29:58 +08:00
```cs
2014-01-01 21:09:41 +08:00
ws.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => {
2013-07-19 16:29:58 +08:00
// Do something to validate the server certificate.
2013-11-26 13:36:44 +08:00
return true; // If the server certificate is valid.
2013-07-19 16:29:58 +08:00
};
```
2013-12-01 13:19:04 +08:00
If you set this property to nothing, the validation does nothing with the server certificate and returns valid.
2013-07-19 16:29:58 +08:00
2014-01-23 14:33:51 +08:00
As a **WebSocket Server** , you should create an instance of the `WebSocketServer` or `HttpServer` class with some settings for the secure connection. It's like the following.
2013-07-19 16:29:58 +08:00
```cs
2013-07-30 15:32:21 +08:00
var wssv = new WebSocketServer (4649, true);
wssv.Certificate = new X509Certificate2 ("/path/to/cert.pfx", "password for cert.pfx");
2013-07-19 16:29:58 +08:00
```
2014-01-18 16:05:57 +08:00
### HTTP Authentication ###
2014-01-22 16:49:38 +08:00
websocket-sharp supports the ** [HTTP Authentication (Basic/Digest)][rfc2617]**.
2014-01-18 16:05:57 +08:00
2014-01-22 16:11:31 +08:00
As a **WebSocket Client** , you should set a pair of user name and password for the HTTP authentication, using the `WebSocket.SetCredentials (string, string, bool)` method before connecting.
2014-01-18 16:05:57 +08:00
```cs
2014-01-22 16:11:31 +08:00
ws.SetCredentials (username, password, preAuth);
2014-01-18 16:05:57 +08:00
```
2014-01-19 15:18:58 +08:00
If `preAuth` is `true` , the `WebSocket` sends the Basic authentication credentials with the first connection request to the server.
2014-01-18 16:05:57 +08:00
2014-01-20 20:30:07 +08:00
Or if `preAuth` is `false` , the `WebSocket` sends either the Basic or Digest authentication (determined by the unauthorized response to the first connection request) credentials with the second connection request to the server.
2014-01-18 16:05:57 +08:00
2014-01-22 16:11:31 +08:00
As a **WebSocket Server** , you should set an HTTP authentication scheme, a realm, and any function to find the user credentials before starting. It's like the following.
2014-01-18 16:05:57 +08:00
```cs
wssv.AuthenticationSchemes = AuthenticationSchemes.Basic;
wssv.Realm = "WebSocket Test";
wssv.UserCredentialsFinder = identity => {
2014-01-22 16:11:31 +08:00
var expected = "nobita";
return identity.Name == expected
2014-01-22 16:49:38 +08:00
? new NetworkCredential (expected, "password", "gunfighter") // User name, password, and roles
2014-01-18 16:05:57 +08:00
: null; // If the user credentials not found.
};
```
2014-01-27 16:05:28 +08:00
If you would like to provide the Digest authentication, you should set like the following.
2014-01-18 16:05:57 +08:00
```cs
wssv.AuthenticationSchemes = AuthenticationSchemes.Digest;
```
2014-03-19 02:25:07 +08:00
### Origin header and Cookies ###
2014-03-29 15:47:57 +08:00
As a **WebSocket Client** , if you would like to send the **Origin header** with the WebSocket connection request to the server, you should set the `WebSocket.Origin` property to an allowable value as the [Origin header] before connecting, like the following.
2014-03-19 02:25:07 +08:00
```cs
ws.Origin = "http://example.com";
```
2014-03-27 15:28:42 +08:00
And if you would like to send the **Cookies** with the WebSocket connection request to the server, you should set any cookie using the `WebSocket.SetCookie (WebSocketSharp.Net.Cookie)` method before connecting, like the following.
2014-03-19 02:25:07 +08:00
```cs
2014-03-29 15:47:57 +08:00
ws.SetCookie (new Cookie ("name", "nobita"));
2014-03-19 02:25:07 +08:00
```
2014-03-29 15:47:57 +08:00
As a **WebSocket Server** , if you would like to check the **Origin header and Cookies** included in each WebSocket connection request, you should set each validation for the Origin header and Cookies in your `WebSocketService` , for example, using the `AddWebSocketService<T> (string, Func<T>)` method with initializing, like the following.
2014-03-19 02:25:07 +08:00
```cs
wssv.AddWebSocketService< Chat > (
"/Chat",
() => new Chat () {
OriginValidator = value => {
// Check 'value' of the Origin header, and return true if it's valid
Uri origin;
return !value.IsNullOrEmpty () & &
Uri.TryCreate (value, UriKind.Absolute, out origin) & &
origin.Host == "example.com";
},
CookiesValidator = (req, res) => {
// Check the Cookies in 'req', and set the Cookies to send to the client with 'res' if necessary
foreach (Cookie cookie in req) {
cookie.Expired = true;
res.Add (cookie);
}
return true; // If the Cookies are valid
}
});
```
2013-07-15 19:42:55 +08:00
### Logging ###
2014-01-22 13:11:05 +08:00
The `WebSocket` class includes the own logging function.
2013-07-15 19:42:55 +08:00
2014-01-23 14:00:51 +08:00
You can access it with the `WebSocket.Log` property (returns a `WebSocketSharp.Logger` ).
2013-07-15 19:42:55 +08:00
2014-03-02 21:15:41 +08:00
So if you would like to change the current logging level (`WebSocketSharp.LogLevel.Error` as the default), you should set the `WebSocket.Log.Level` property to any of the `LogLevel` enum values.
2013-07-15 19:42:55 +08:00
```cs
2014-03-02 21:15:41 +08:00
ws.Log.Level = LogLevel.Debug;
2013-07-15 19:42:55 +08:00
```
2014-03-03 16:22:29 +08:00
This means a log with lower than `LogLevel.Debug` cannot be outputted.
2013-07-15 19:42:55 +08:00
2014-03-02 21:15:41 +08:00
And if you would like to output a log, you should use any of the output methods. The following outputs a log with `LogLevel.Debug` .
2013-07-15 19:42:55 +08:00
```cs
2013-07-30 15:32:21 +08:00
ws.Log.Debug ("This is a debug message.");
2013-07-15 19:42:55 +08:00
```
2014-01-22 02:53:20 +08:00
The `WebSocketServer` and `HttpServer` classes include the same logging function.
2013-07-15 19:42:55 +08:00
2012-07-31 09:36:52 +08:00
## Examples ##
2013-07-30 15:32:21 +08:00
Examples using **websocket-sharp** .
2012-07-31 09:36:52 +08:00
### Example ###
2014-01-22 13:34:00 +08:00
**[Example]** connects to the ** [Echo server]** with the WebSocket.
2012-07-31 09:36:52 +08:00
### Example1 ###
2014-01-23 14:19:46 +08:00
**[Example1]** connects to the ** [Audio Data delivery server]** with the WebSocket. (But it's only implemented the chat feature, still unfinished.)
2012-07-31 09:36:52 +08:00
2014-01-22 13:34:00 +08:00
And Example1 uses ** [Json.NET]**.
2012-07-31 09:36:52 +08:00
2012-08-04 14:51:31 +08:00
### Example2 ###
2014-01-22 13:34:00 +08:00
**[Example2]** starts a WebSocket server.
2012-09-10 00:36:22 +08:00
### Example3 ###
2014-02-04 16:32:03 +08:00
**[Example3]** starts an HTTP server that allows to accept the WebSocket connection requests.
2012-08-04 14:51:31 +08:00
2014-01-22 13:34:00 +08:00
Could you access to [http://localhost:4649 ](http://localhost:4649 ) to do **WebSocket Echo Test** with your web browser after Example3 running?
2012-09-24 14:01:25 +08:00
2013-05-01 13:27:24 +08:00
## Supported WebSocket Specifications ##
2012-07-31 09:36:52 +08:00
2014-03-19 02:25:07 +08:00
websocket-sharp supports ** [RFC 6455][rfc6455]**, and it's based on the following WebSocket references:
2013-10-28 15:29:51 +08:00
2013-11-19 16:15:08 +08:00
- **[The WebSocket Protocol][rfc6455]**
- **[The WebSocket API][api]**
2013-10-28 15:29:51 +08:00
- **[Compression Extensions for WebSocket][compression]**
Thanks for translating to japanese.
2013-11-19 16:15:08 +08:00
- **[The WebSocket Protocol 日本語訳][rfc6455_ja]**
- **[The WebSocket API 日本語訳][api_ja]**
2013-10-28 15:29:51 +08:00
2012-07-31 09:36:52 +08:00
## License ##
2014-01-22 13:44:42 +08:00
websocket-sharp is provided under ** [The MIT License]**.
2012-07-31 09:36:52 +08:00
2013-11-18 02:17:18 +08:00
[Audio Data delivery server]: http://agektmr.node-ninja.com:3000
2012-07-31 09:36:52 +08:00
[Echo server]: http://www.websocket.org/echo.html
[Example]: https://github.com/sta/websocket-sharp/tree/master/Example
[Example1]: https://github.com/sta/websocket-sharp/tree/master/Example1
2012-08-04 14:51:31 +08:00
[Example2]: https://github.com/sta/websocket-sharp/tree/master/Example2
2012-09-10 00:36:22 +08:00
[Example3]: https://github.com/sta/websocket-sharp/tree/master/Example3
2012-07-31 09:36:52 +08:00
[Json.NET]: http://james.newtonking.com/projects/json-net.aspx
2013-11-18 02:17:18 +08:00
[Mono]: http://www.mono-project.com
2013-11-17 16:03:52 +08:00
[MonoDevelop]: http://monodevelop.com
2013-11-17 17:10:21 +08:00
[NuGet Gallery]: http://www.nuget.org
[NuGet Gallery: websocket-sharp]: http://www.nuget.org/packages/WebSocketSharp
2014-03-19 02:25:07 +08:00
[Origin header]: http://tools.ietf.org/html/rfc6454#section-7
2013-11-19 16:15:08 +08:00
[The MIT License]: https://raw.github.com/sta/websocket-sharp/master/LICENSE.txt
2013-11-18 03:59:29 +08:00
[Unity]: http://unity3d.com
2014-01-19 17:12:38 +08:00
[WebSocket-Sharp for Unity]: http://u3d.as/content/sta-blockhead/websocket-sharp-for-unity
[api]: http://www.w3.org/TR/websockets
2013-11-19 16:15:08 +08:00
[api_ja]: http://www.hcn.zaq.ne.jp/___/WEB/WebSocket-ja.html
2013-10-28 14:34:21 +08:00
[compression]: http://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-09
2013-11-22 15:15:44 +08:00
[draft-hixie-thewebsocketprotocol-75]: http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75
[draft-ietf-hybi-thewebsocketprotocol-00]: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00
[draft75]: https://github.com/sta/websocket-sharp/tree/draft75
2013-10-28 14:34:21 +08:00
[extension parameters]: http://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-09#section-8.1
2013-11-22 15:15:44 +08:00
[hybi-00]: https://github.com/sta/websocket-sharp/tree/hybi-00
[master]: https://github.com/sta/websocket-sharp/tree/master
2014-01-22 16:49:38 +08:00
[rfc2617]: http://tools.ietf.org/html/rfc2617
2013-11-19 16:15:08 +08:00
[rfc6455]: http://tools.ietf.org/html/rfc6455
[rfc6455_ja]: http://www.hcn.zaq.ne.jp/___/WEB/RFC6455-ja.html