websocket-sharp/README.md

475 lines
12 KiB
Markdown
Raw Normal View History

2013-05-21 20:03:11 +08:00
<!-- # websocket-sharp # -->
![Logo](websocket-sharp.png)
2010-10-19 10:27:39 +08:00
2013-07-15 19:42:55 +08:00
**websocket-sharp** is a C# implementation of the **WebSocket** protocol client and server.
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;
namespace Example {
public class Program {
public static void Main(string[] args)
{
using (var ws = new WebSocket("ws://dragonsnest.far/Laputa"))
{
ws.OnMessage += (sender, e) =>
{
Console.WriteLine("Laputa says: {0}", e.Data);
};
ws.Connect();
ws.Send("BALUS");
Console.ReadKey(true);
}
}
}
}
```
2012-08-04 14:51:31 +08:00
#### Step 1 ####
2010-10-19 10:27:39 +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
2013-07-15 19:42:55 +08:00
Creating a instance of the `WebSocket` class with the specified WebSocket URL to connect.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
2013-04-12 23:41:04 +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
The `WebSocket` class inherits the `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
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
ws.OnOpen += (sender, e) =>
{
...
};
```
2012-07-31 09:36:52 +08:00
2013-07-19 16:29:58 +08:00
`e` has come across as `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
2013-07-15 19:42:55 +08:00
A `WebSocket.OnMessage` event occurs when the `WebSocket` receives a WebSocket data frame.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
ws.OnMessage += (sender, e) =>
{
...
};
```
2012-07-31 09:36:52 +08:00
2013-07-19 16:29:58 +08:00
`e.Type` (`WebSocketSharp.MessageEventArgs.Type`, the type of this property is `WebSocketSharp.Opcode`) indicates the **Frame Type** of a WebSocket frame, so by checking this property, you determine which item you should use.
2013-07-24 22:05:33 +08:00
If `e.Type` equals `Opcode.TEXT`, you use `e.Data` (`WebSocketSharp.MessageEventArgs.Data`, the type of this property is `string`) that contains the received **Text** data.
2013-07-19 16:29:58 +08:00
2013-07-24 22:05:33 +08:00
If `e.Type` equals `Opcode.BINARY`, you use `e.RawData` (`WebSocketSharp.MessageEventArgs.RawData`, the type of this property is `byte[]`) that contains the received **Binary** data.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
2013-07-15 19:42:55 +08:00
if (e.Type == Opcode.TEXT)
2012-08-08 10:32:19 +08:00
{
2013-07-15 19:42:55 +08:00
// Do something with e.Data
return;
}
if (e.Type == Opcode.BINARY)
{
// 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
ws.OnError += (sender, e) =>
{
...
};
```
2013-07-19 16:29:58 +08:00
`e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, the type of this property is `string`) contains an error message, so you use this.
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
ws.OnClose += (sender, e) =>
{
...
};
```
2012-07-31 09:36:52 +08:00
2013-07-19 16:29:58 +08:00
`e.Code` (`WebSocketSharp.CloseEventArgs.Code`, the type of this property is `ushort`) contains a status code indicating the reason for closure and `e.Reason` (`WebSocketSharp.CloseEventArgs.Reason`, the type of this property is `string`) contains the reason for closure, so you use these.
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
Connecting to the WebSocket server.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
ws.Connect();
```
2012-07-31 09:36:52 +08:00
2012-08-04 14:51:31 +08:00
#### Step 5 ####
2012-07-31 09:36:52 +08:00
Sending a data.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
ws.Send(data);
```
2012-07-31 09:36:52 +08:00
The `Send` method is overloaded.
2012-07-31 09:36:52 +08:00
2013-07-01 11:18:26 +08:00
The types of `data` are `string`, `byte[]` and `FileInfo` class.
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
Closing the WebSocket connection.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
ws.Close(code, reason);
```
2012-07-31 09:36:52 +08:00
2013-07-19 16:29:58 +08:00
If you want to close the WebSocket connection explicitly, you can use the `Close` method.
2012-07-31 09:36:52 +08:00
2013-07-15 19:42:55 +08:00
And the `Close` method is overloaded. The types of `code` are `WebSocketSharp.CloseStatusCode` and `ushort`, the type of `reason` is `string`.
2012-07-31 09:36:52 +08:00
In addition, the `Close()` and `Close(code)` methods exist.
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;
namespace Example {
public class Laputa : WebSocketService
{
protected override void OnMessage(MessageEventArgs e)
{
2013-07-19 16:29:58 +08:00
var msg = e.Data.ToLower() == "balus"
2013-04-12 23:41:04 +08:00
? "I've been balused already..."
: "I'm not available now.";
Send(msg);
}
}
public class Program {
public static void Main(string[] args)
{
var wssv = new WebSocketServiceHost<Laputa>("ws://dragonsnest.far/Laputa");
wssv.Start();
Console.ReadKey(true);
wssv.Stop();
}
}
}
```
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
The `WebSocketServer`, `WebSocketServiceHost<T>` and `WebSocketService` classes exist in the `WebSocketSharp.Server` namespace.
2012-08-04 14:51:31 +08:00
#### Step 2 ####
Creating a class that inherits the `WebSocketService` class.
2012-08-04 14:51:31 +08:00
2012-08-06 13:34:39 +08:00
For example, if you want to provide the 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
{
protected override void OnMessage(MessageEventArgs e)
2012-08-08 10:20:57 +08:00
{
Send(e.Data);
}
}
```
2012-08-04 14:51:31 +08:00
Or if you want to provide the 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
{
protected override void OnMessage(MessageEventArgs e)
2012-08-08 10:32:19 +08:00
{
Broadcast(e.Data);
2012-08-08 10:32:19 +08:00
}
}
```
2012-08-04 14:51:31 +08:00
If you override the `OnMessage` method, it is bound to the server side `WebSocket.OnMessage` event.
In addition, if you override the `OnOpen`, `OnError` and `OnClose` methods, each of them is bound to the `WebSocket.OnOpen`, `WebSocket.OnError` and `WebSocket.OnClose` events.
2012-08-06 13:34:39 +08:00
#### Step 3 ####
2012-08-04 14:51:31 +08:00
Creating a instance of the `WebSocketServiceHost<T>` class if you want the single WebSocket service server.
2012-08-04 14:51:31 +08:00
2012-08-08 10:32:19 +08:00
```cs
var wssv = new WebSocketServiceHost<Echo>("ws://example.com:4649");
2012-08-08 10:32:19 +08:00
```
2012-08-04 14:51:31 +08:00
2013-07-15 19:42:55 +08:00
Or creating a instance of the `WebSocketServer` class if you want the multi WebSocket service server.
```cs
var wssv = new WebSocketServer(4649);
wssv.AddWebSocketService<Echo>("/Echo");
wssv.AddWebSocketService<Chat>("/Chat");
```
2013-07-15 19:42:55 +08:00
You can add any WebSocket service with a specified path to the service to your `WebSocketServer` by using the `WebSocketServer.AddWebSocketService<T>` method.
The type of `T` inherits `WebSocketService` class, so you can use a class that was created in **Step 2**.
2012-08-04 14:51:31 +08:00
2013-07-19 16:29:58 +08:00
If you create a instance of the `WebSocketServer` class without the port number, the `WebSocketServer` set the port number to **80** automatically. So it is 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
Setting the event.
2012-08-04 14:51:31 +08:00
2013-07-24 22:05:33 +08:00
##### WebSocketServiceHost&lt;T>.OnError Event #####
2012-08-04 14:51:31 +08:00
2013-07-15 19:42:55 +08:00
A `WebSocketServiceHost<T>.OnError` event occurs when the `WebSocketServiceHost<T>` gets an error.
2012-08-04 14:51:31 +08:00
2012-08-08 10:32:19 +08:00
```cs
wssv.OnError += (sender, e) =>
{
...
};
```
2012-08-04 14:51:31 +08:00
2013-07-19 16:29:58 +08:00
`e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, the type of this property is `string`) contains an error message, so you use this.
2012-08-04 14:51:31 +08:00
2013-07-24 22:05:33 +08:00
##### WebSocketServer.OnError Event #####
2012-10-01 14:47:13 +08:00
Same as the `WebSocketServiceHost<T>.OnError` event.
2012-10-01 14:47:13 +08:00
2012-08-06 13:34:39 +08:00
#### Step 5 ####
2012-08-04 14:51:31 +08:00
Starting the server.
2012-08-04 14:51:31 +08:00
2012-08-08 10:32:19 +08:00
```cs
wssv.Start();
```
2012-08-04 14:51:31 +08:00
#### Step 6 ####
Stopping the server.
2012-08-04 14:51:31 +08:00
2012-08-08 10:32:19 +08:00
```cs
wssv.Stop();
```
2012-08-04 14:51:31 +08:00
2013-07-01 11:18:26 +08:00
### HTTP Server with the WebSocket ###
2012-09-10 00:36:22 +08:00
2013-07-15 19:42:55 +08:00
I modified the `System.Net.HttpListener`, `System.Net.HttpListenerContext` and some other classes of [Mono] to create the HTTP server that can upgrade the connection to the WebSocket connection when receives a WebSocket connection request.
2012-09-10 00:36:22 +08:00
2013-07-15 19:42:55 +08:00
You can add any WebSocket service with a specified path to the service to your `HttpServer` by using the `HttpServer.AddWebSocketService<T>` method.
2012-09-10 00:36:22 +08:00
```cs
var httpsv = new HttpServer(4649);
httpsv.AddWebSocketService<Echo>("/");
2012-09-10 00:36:22 +08:00
```
2013-07-01 11:18:26 +08:00
For more information, could you see **[Example3]**?
2013-07-19 16:29:58 +08:00
### Secure Connection ###
2013-07-24 22:05:33 +08:00
As a **WebSocket Client**, creating a instance of the `WebSocket` class with the WebSocket URL with **wss** scheme.
2013-07-19 16:29:58 +08:00
```cs
using (var ws = new WebSocket("wss://example.com"))
{
...
}
```
If you want to set the custom validation for the server certificate, you can use the `WebSocket.ServerCertificateValidationCallback` property.
```cs
ws.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
{
// Do something to validate the server certificate.
return true; // The server certificate is valid.
};
```
If you set this property to nothing, the validation does nothing with the server certificate, always returns valid.
As a **WebSocket Server**, creating and setting a instance of the WebSocket server with some settings for the secure connection.
```cs
var wssv = new WebSocketServer(4649, true);
wssv.Certificate = new X509Certificate2("/path/to/cert.pfx", "password for cert.pfx");
```
2013-07-15 19:42:55 +08:00
### Logging ###
The `WebSocket` class includes own logging functions.
The `WebSocket.Log` property provides the logging functions.
2013-07-19 16:29:58 +08:00
If you want to change the current logging level (the default is `LogLevel.ERROR`), you can use the `WebSocket.Log.Level` property.
2013-07-15 19:42:55 +08:00
```cs
ws.Log.Level = LogLevel.DEBUG;
```
2013-07-19 16:29:58 +08:00
This setting means that the logging outputs with a less than `LogLevel.DEBUG` are not outputted.
2013-07-15 19:42:55 +08:00
2013-07-19 16:29:58 +08:00
And if you want to output a log, you can use some output methods. The following outputs a log with `LogLevel.DEBUG`.
2013-07-15 19:42:55 +08:00
```cs
ws.Log.Debug("This is a debug message.");
```
The `WebSocketServiceHost<T>`, `WebSocketServer` and `HttpServer` classes include the same logging functions.
2012-07-31 09:36:52 +08:00
## Examples ##
Examples of using **websocket-sharp**.
### Example ###
[Example] connects to the [Echo server] using the WebSocket.
### Example1 ###
2013-07-15 19:42:55 +08:00
[Example1] connects to the [Audio Data delivery server] using the WebSocket ([Example1] is only implemented the chat feature, still unfinished).
2012-07-31 09:36:52 +08:00
2013-07-19 16:29:58 +08:00
And [Example1] uses [Json.NET].
2012-07-31 09:36:52 +08:00
2012-08-04 14:51:31 +08:00
### Example2 ###
2013-07-15 19:42:55 +08:00
[Example2] starts a WebSocket server.
2012-09-10 00:36:22 +08:00
### Example3 ###
2013-07-15 19:42:55 +08:00
[Example3] starts an HTTP server that can upgrade the connection to the WebSocket connection.
2012-08-04 14:51:31 +08:00
2013-07-15 19:42:55 +08:00
Could you access to [http://localhost:4649](http://localhost:4649) to do **WebSocket Echo Test** with your web browser after [Example3] running?
2013-07-02 10:25:26 +08:00
## websocket-sharp for Unity ##
**websocket-sharp** has now been displayed on the **Unity Asset Store**!
- **[websocket-sharp for Unity]**
2013-07-05 23:34:27 +08:00
## Required Environment ##
C# **3.0**, .NET **3.5** compatible or later.
2013-05-01 13:27:24 +08:00
## Supported WebSocket Specifications ##
2012-07-31 09:36:52 +08:00
**websocket-sharp** supports **[RFC 6455]**.
2012-12-05 21:10:15 +08:00
- **[branch: hybi-00]** supports older draft-ietf-hybi-thewebsocketprotocol-00 ( **[hybi-00]** ).
- **[branch: draft75]** supports even more old draft-hixie-thewebsocketprotocol-75 ( **[hixie-75]** ).
2012-07-31 09:36:52 +08:00
## Supported WebSocket Extensions ##
### Per-message Compression ###
2013-05-01 13:27:24 +08:00
**websocket-sharp** supports **[Per-message Compression][compression]** extension. (But, does not support with [extension parameters].)
If you want to enable this extension as a WebSocket client, you should do like the following.
```cs
ws.Compression = CompressionMethod.DEFLATE;
```
And then your client sends the following header in the opening handshake to a WebSocket server.
```
2013-05-01 13:27:24 +08:00
Sec-WebSocket-Extensions: permessage-deflate
```
If the server supports this extension, responds the same header. And when your client receives the header, enables this extension.
2013-07-02 10:25:26 +08:00
## WebSocket References ##
2012-07-31 09:36:52 +08:00
- **[The WebSocket Protocol]**
- **[The WebSocket API]**
2013-05-01 13:27:24 +08:00
- **[Compression Extensions for WebSocket][compression]**
2012-07-31 09:36:52 +08:00
Thanks for translating to japanese.
2012-07-31 09:36:52 +08:00
- **[The WebSocket Protocol 日本語訳]**
- **[The WebSocket API 日本語訳]**
## License ##
2013-01-22 19:46:32 +08:00
Copyright &copy; 2010 - 2013 sta.blockhead
2012-07-31 09:36:52 +08:00
Licensed under the **[MIT License]**.
[Audio Data delivery server]: http://agektmr.node-ninja.com:3000/
[branch: draft75]: https://github.com/sta/websocket-sharp/tree/draft75
[branch: hybi-00]: https://github.com/sta/websocket-sharp/tree/hybi-00
2013-05-01 13:27:24 +08:00
[compression]: http://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-09
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
2013-05-01 13:27:24 +08:00
[extension parameters]: http://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-09#section-8.1
2012-07-31 09:36:52 +08:00
[hixie-75]: http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75
[hybi-00]: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00
[Json.NET]: http://james.newtonking.com/projects/json-net.aspx
[MIT License]: http://www.opensource.org/licenses/mit-license.php
2012-09-10 00:36:22 +08:00
[Mono]: http://www.mono-project.com/
2012-07-31 09:36:52 +08:00
[RFC 6455]: http://tools.ietf.org/html/rfc6455
[The WebSocket API]: http://www.w3.org/TR/websockets/
2012-07-31 09:36:52 +08:00
[The WebSocket API 日本語訳]: http://www.hcn.zaq.ne.jp/___/WEB/WebSocket-ja.html
[The WebSocket Protocol]: http://tools.ietf.org/html/rfc6455
[The WebSocket Protocol 日本語訳]: http://www.hcn.zaq.ne.jp/___/WEB/RFC6455-ja.html
2013-07-02 10:25:26 +08:00
[websocket-sharp for Unity]: http://u3d.as/content/sta-blockhead/websocket-sharp-for-unity