2010-10-19 10:27:39 +08:00
# websocket-sharp #
2012-10-12 13:53:29 +08:00
**websocket-sharp** is a C# implementation of the WebSocket protocol client & server.
2010-10-19 10:27:39 +08:00
## Usage ##
2012-10-12 13:53:29 +08:00
### The WebSocket client ###
2012-08-04 14:51:31 +08:00
#### Step 1 ####
2010-10-19 10:27:39 +08:00
2012-07-31 09:36:52 +08:00
Required namespaces.
2012-08-08 10:32:19 +08:00
```cs
using WebSocketSharp;
using WebSocketSharp.Frame;
```
2012-07-31 09:36:52 +08:00
2012-10-12 13:53:29 +08:00
The `WebSocket` class exists in the `WebSocketSharp` namespace, the WebSocket frame resources (e.g. `WsFrame` class) exist in the `WebSocketSharp.Frame` 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
2012-10-12 13:53:29 +08:00
Creating a instance of the `WebSocket` class.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
using (WebSocket ws = new WebSocket("ws://example.com"))
{
...
}
```
2012-07-31 09:36:52 +08:00
2012-10-12 13:53:29 +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
2012-10-12 13:53:29 +08:00
Setting the `WebSocket` events.
2012-07-31 09:36:52 +08:00
2012-08-04 14:51:31 +08:00
##### WebSocket.OnOpen event #####
2012-07-31 09:36:52 +08:00
2012-10-12 13:53:29 +08:00
The `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
2012-10-12 13:53:29 +08:00
The `e` has come across as the `EventArgs.Empty` , so there is no operation on the `e` .
2012-07-31 09:36:52 +08:00
2012-08-04 14:51:31 +08:00
##### WebSocket.OnMessage event #####
2012-07-31 09:36:52 +08:00
2012-10-12 13:53:29 +08:00
The `WebSocket.OnMessage` event occurs when the `WebSocket` receives a 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
2012-10-12 13:53:29 +08:00
The `e.Type` (`WebSocketSharp.MessageEventArgs.Type`, its type is `WebSocketSharp.Frame.Opcode` ) contains the **Frame type** of the data frame, so you check it out and you determine which item you should operate.
2012-07-31 09:36:52 +08:00
2012-08-08 10:32:19 +08:00
```cs
switch (e.Type)
{
case Opcode.TEXT:
...
break;
case Opcode.BINARY:
...
break;
default:
break;
2012-08-08 10:36:17 +08:00
}
2012-08-08 10:32:19 +08:00
```
2012-07-31 09:36:52 +08:00
2012-10-12 13:53:29 +08:00
If the `e.Type` is `Opcode.TEXT` , you operate the `e.Data` (`WebSocketSharp.MessageEventArgs.Data`, its type is `string` ).
2012-07-31 09:36:52 +08:00
2012-10-12 13:53:29 +08:00
If the `e.Type` is `Opcode.BINARY` , you operate the `e.RawData` (`WebSocketSharp.MessageEventArgs.RawData`, its type is `byte[]` ).
2012-07-31 09:36:52 +08:00
2012-08-04 14:51:31 +08:00
##### WebSocket.OnError event #####
2012-07-31 09:36:52 +08:00
2012-10-12 13:53:29 +08:00
The `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) =>
{
...
};
```
2012-10-12 13:53:29 +08:00
The `e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, its type is `string` ) contains the error message, so you operate it.
2012-07-31 09:36:52 +08:00
2012-08-04 14:51:31 +08:00
##### WebSocket.OnClose event #####
2012-07-31 09:36:52 +08:00
2012-10-12 13:53:29 +08:00
The `WebSocket.OnClose` event occurs when the `WebSocket` receives a Close frame or the `Close` method is called.
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
2012-10-13 16:38:19 +08:00
The `e.Code` (`WebSocketSharp.CloseEventArgs.Code`, its type is `WebSocketSharp.Frame.CloseStatusCode` ) contains the close status code and the `e.Reason` (`WebSocketSharp.CloseEventArgs.Reason`, its type is `string` ) contains the reason why closes, so you operate them.
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
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
2012-10-12 13:53:29 +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
2012-10-12 13:53:29 +08:00
The `Send` method is overloaded.
2012-07-31 09:36:52 +08:00
2012-10-12 13:53:29 +08:00
The types of `data` are `string` , `byte[]` or `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
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
ws.Close(code, reason);
```
2012-07-31 09:36:52 +08:00
2012-10-12 13:53:29 +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
2012-10-12 13:53:29 +08:00
The type of `code` is `WebSocketSharp.Frame.CloseStatusCode` , the type of `reason` is `string` .
2012-07-31 09:36:52 +08:00
2012-10-12 13:53:29 +08:00
The `Close` method is overloaded. (In addition, the `Close()` and `Close(code)` methods exist.)
2012-07-31 09:36:52 +08:00
2012-10-12 13:53:29 +08:00
### The WebSocket server ###
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
2012-10-12 13:53:29 +08:00
The `WebSocketServer` , `WebSocketServer<T>` and `WebSocketService` classes exist in the `WebSocketSharp.Server` namespace.
2012-08-04 14:51:31 +08:00
#### Step 2 ####
2012-10-12 13:53:29 +08:00
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(object sender, MessageEventArgs e)
{
Send(e.Data);
}
}
```
2012-08-04 14:51:31 +08:00
2012-10-12 13:53:29 +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(object sender, MessageEventArgs e)
{
Publish(e.Data);
}
}
```
2012-08-04 14:51:31 +08:00
2012-10-12 13:53:29 +08:00
If you override the `onMessage` method, it is bound to the server side `WebSocket.OnMessage` event.
2012-08-13 11:31:11 +08:00
2012-10-12 13:53:29 +08:00
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-13 11:31:11 +08:00
2012-08-06 13:34:39 +08:00
#### Step 3 ####
2012-08-04 14:51:31 +08:00
2012-10-12 13:53:29 +08:00
Creating a instance of the `WebSocketServer<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 WebSocketServer< Echo > ("ws://example.com:4649");
```
2012-08-04 14:51:31 +08:00
2012-10-12 13:53:29 +08:00
Creating a instance of the `WebSocketServer` class if you want the multi WebSocket service server.
2012-09-24 14:01:25 +08:00
```cs
var wssv = new WebSocketServer(4649);
wssv.AddService< Echo > ("/Echo");
wssv.AddService< Chat > ("/Chat");
```
2012-10-12 13:53:29 +08:00
You can add to your `WebSocketServer` any WebSocket service and a matching path to that service by using the `WebSocketServer.AddService<T>` method.
2012-09-24 14:01:25 +08:00
2012-10-12 13:53:29 +08:00
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
2012-10-12 13:53:29 +08:00
If you create a instance of the `WebSocketServer` class without port number, `WebSocketServer` set **80** to port number automatically.
2012-08-06 13:34:39 +08:00
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
2012-10-12 13:53:29 +08:00
Setting the event.
2012-08-04 14:51:31 +08:00
2012-10-01 14:59:27 +08:00
##### WebSocketServer<T>.OnError event #####
2012-08-04 14:51:31 +08:00
2012-10-12 13:53:29 +08:00
The `WebSocketServer<T>.OnError` event occurs when the `WebSocketServer<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
2012-10-12 13:53:29 +08:00
The `e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, its type is `string` ) contains the error message, so you operate it.
2012-08-04 14:51:31 +08:00
2012-10-01 14:47:13 +08:00
##### WebSocketServer.OnError event #####
2012-10-12 13:53:29 +08:00
Same as the `WebSocketServer<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
2012-10-12 13:53:29 +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 ####
2012-10-12 13:53:29 +08:00
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
2012-10-12 13:53:29 +08:00
### The HTTP server with the WebSocket ###
2012-09-10 00:36:22 +08:00
2012-10-12 13:53:29 +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 request.
2012-09-10 00:36:22 +08:00
2012-10-12 13:53:29 +08:00
You can add to your `HttpServer` any WebSocket service and a matching path to that service by using the `HttpServer.AddService<T>` method.
2012-09-10 00:36:22 +08:00
```cs
2012-09-19 13:40:12 +08:00
var httpsv = new HttpServer(4649);
httpsv.AddService< Echo > ("/");
2012-09-10 00:36:22 +08:00
```
For more information, please refer to the [Example3].
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 ###
[Example1] connects to the [Audio Data delivery server] using the WebSocket ([Example1] is only implemented a chat feature, still unfinished).
[Example1] uses [Json.NET].
2012-08-04 14:51:31 +08:00
### Example2 ###
2012-09-10 00:36:22 +08:00
[Example2] starts the WebSocket server.
### Example3 ###
2012-10-12 13:53:29 +08:00
[Example3] starts the HTTP server that can upgrade the connection to the WebSocket connection.
2012-08-04 14:51:31 +08:00
2012-10-01 14:47:13 +08:00
Please access [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
2012-07-31 09:36:52 +08:00
## Supported WebSocket Protocol ##
**websocket-sharp** supports ** [RFC 6455]**.
- @**[branch: hybi-00]** supports older draft-ietf-hybi-thewebsocketprotocol-00 (**[hybi-00]**).
- @**[branch: draft75]** supports even more old draft-hixie-thewebsocketprotocol-75 (**[hixie-75]**).
## Reference ##
- **[The WebSocket Protocol]**
- **[The WebSocket API]**
Thx for translating to japanese.
- **[The WebSocket Protocol 日本語訳]**
- **[The WebSocket API 日本語訳]**
## License ##
Copyright © 2010 - 2012 sta.blockhead
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
[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
[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
2012-10-12 13:53:29 +08:00
[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