websocket-sharp/README.md

259 lines
6.6 KiB
Markdown
Raw Normal View History

2010-10-19 10:27:39 +08:00
# websocket-sharp #
2012-08-04 15:13:03 +08:00
**websocket-sharp** is a C# implementation of WebSocket protocol client & server.
2010-10-19 10:27:39 +08:00
## Usage ##
2012-08-04 14:51:31 +08:00
### WebSocket Client ###
#### Step 1 ####
2010-10-19 10:27:39 +08:00
2012-07-31 09:36:52 +08:00
Required namespaces.
using WebSocketSharp;
using WebSocketSharp.Frame;
2012-08-04 15:13:03 +08:00
`WebSocket` class exists in `WebSocketSharp` namespace, WebSocket data frame resources (e.g. `WsFrame` class) exist in `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
Creating instance of `WebSocket` class.
using (WebSocket ws = new WebSocket("ws://example.com"))
{
...
}
2012-08-04 14:51:31 +08:00
`WebSocket` class inherits `IDisposable` interface, so you can use `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-08-01 22:22:30 +08:00
Setting `WebSocket` event handlers.
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
`WebSocket.OnOpen` event is emitted immediately after WebSocket connection has been established.
ws.OnOpen += (sender, e) =>
{
...
};
2012-08-04 14:51:31 +08:00
`e` has come across as `EventArgs.Empty`, so there is no operation on `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
`WebSocket.OnMessage` event is emitted each time WebSocket data frame is received.
ws.OnMessage += (sender, e) =>
{
...
};
2012-08-04 15:31:16 +08:00
**Frame type** of received WebSocket data frame is stored in `e.Type` (`WebSocketSharp.MessageEventArgs.Type`, its type is `WebSocketSharp.Frame.Opcode`), so you check it out and you determine which item you should operate.
2012-07-31 09:36:52 +08:00
switch (e.Type)
{
case Opcode.TEXT:
...
break;
case Opcode.BINARY:
...
break;
default:
break;
}
If `e.Type` is `Opcode.TEXT`, you operate `e.Data` (`WebSocketSharp.MessageEventArgs.Data`, its type is `string`).
2012-08-01 22:22:30 +08:00
If `e.Type` is `Opcode.BINARY`, you operate `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
`WebSocket.OnError` event is emitted when some error is occurred.
ws.OnError += (sender, e) =>
{
...
};
2012-08-04 14:51:31 +08:00
Error message is stored in `e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, its type is `string`), 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
`WebSocket.OnClose` event is emitted when WebSocket connection is closed.
ws.OnClose += (sender, e) =>
{
...
};
2012-08-04 14:51:31 +08:00
Close status code is stored in `e.Code` (`WebSocketSharp.CloseEventArgs.Code`, its type is `WebSocketSharp.Frame.CloseStatusCode`) and reason of close is stored in `e.Reason` (`WebSocketSharp.CloseEventArgs.Reason`, its type is `string`), 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
Connecting to server using WebSocket.
ws.Connect();
2012-08-04 14:51:31 +08:00
#### Step 5 ####
2012-07-31 09:36:52 +08:00
Sending data.
ws.Send(data);
`WebSocket.Send` method is overloaded.
2012-08-01 22:22:30 +08:00
`data` types 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 WebSocket connection.
ws.Close(code, reason);
If you want to close WebSocket connection explicitly, you can use `Close` method.
Type of `code` is `WebSocketSharp.Frame.CloseStatusCode`, type of `reason` is `string`.
`WebSocket.Close` method is overloaded (In addition `Close()` and `Close(code)` exist).
2012-08-04 14:51:31 +08:00
### WebSocket Server ###
#### Step 1 ####
Required namespaces.
2012-08-06 13:34:39 +08:00
using WebSocketSharp.Server;
2012-08-04 14:51:31 +08:00
2012-08-06 13:34:39 +08:00
`WebSocketServer<T>` class exists in `WebSocketSharp.Server` namespace.
2012-08-04 14:51:31 +08:00
#### Step 2 ####
2012-08-06 13:34:39 +08:00
Creating a class that inherits from `WebSocketService`.
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-06 13:34:39 +08:00
using WebSocketSharp;
using WebSocketSharp.Server;
2012-08-04 14:51:31 +08:00
2012-08-06 13:34:39 +08:00
public class Echo : WebSocketService
{
protected override void onMessage(object sender, MessageEventArgs e)
{
Send(e.Data);
}
}
2012-08-04 14:51:31 +08:00
2012-08-06 13:34:39 +08:00
For example, if you want to provide the chat service,
2012-08-04 14:51:31 +08:00
2012-08-06 13:34:39 +08:00
using WebSocketSharp;
using WebSocketSharp.Server;
2012-08-04 14:51:31 +08:00
2012-08-06 13:34:39 +08:00
public class Chat : WebSocketService
2012-08-04 14:51:31 +08:00
{
2012-08-06 13:34:39 +08:00
protected override void onMessage(object sender, MessageEventArgs e)
{
Server.Send(e.Data);
}
}
2012-08-04 14:51:31 +08:00
2012-08-06 13:34:39 +08:00
#### Step 3 ####
2012-08-04 14:51:31 +08:00
2012-08-06 13:34:39 +08:00
Creating instance of `WebSocketServer<T>` class.
2012-08-04 14:51:31 +08:00
2012-08-06 13:34:39 +08:00
var wssv = new WebSocketServer<Echo>("ws://example.com:4649");
2012-08-04 14:51:31 +08:00
2012-08-06 13:34:39 +08:00
Type of `T` inherits from `WebSocketService` class, so you can use a class that was created in **Step 2**.
2012-08-04 14:51:31 +08:00
2012-08-06 13:34:39 +08:00
If you set WebSocket url without port number, `WebSocketServer<T>` set 80 or 443 to port number 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
2012-08-06 13:34:39 +08:00
Setting WebSocketServer event handler.
2012-08-04 14:51:31 +08:00
##### WebSocketServer.OnError event #####
`WebSocketServer.OnError` event is emitted when some error is occurred.
wssv.OnError += (sender, e) =>
{
...
};
Error message is stored in `e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, its type is `string`), so you operate it.
2012-08-06 13:34:39 +08:00
#### Step 5 ####
2012-08-04 14:51:31 +08:00
Starting server.
wssv.Start();
#### Step 6 ####
Stopping server.
wssv.Stop();
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 ###
[Example2] starts WebSocket server.
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 &copy; 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-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
[RFC 6455]: http://tools.ietf.org/html/rfc6455
[The WebSocket API]: http://dev.w3.org/html5/websockets
[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