`WebSocket` class exists in `WebSocketSharp` namespace, WebSocket data frame resources (e.g. `WsFrame` class) exist in `WebSocketSharp.Frame` namespace.
`WebSocket` class exists in `WebSocketSharp` namespace, WebSocket data frame resources (e.g. `WsFrame` class) exist in `WebSocketSharp.Frame` namespace.
@ -19,10 +21,12 @@ Required namespaces.
Creating a instance of `WebSocket` class.
Creating a instance of `WebSocket` class.
```cs
using (WebSocket ws = new WebSocket("ws://example.com"))
using (WebSocket ws = new WebSocket("ws://example.com"))
{
{
...
...
}
}
```
`WebSocket` class inherits `IDisposable` interface, so you can use `using` statement.
`WebSocket` class inherits `IDisposable` interface, so you can use `using` statement.
`WebSocket.OnMessage` event is emitted each time WebSocket data frame is received.
`WebSocket.OnMessage` event is emitted each time WebSocket data frame is received.
```cs
ws.OnMessage += (sender, e) =>
ws.OnMessage += (sender, e) =>
{
{
...
...
};
};
```
**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.
**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.
If `e.Type` is `Opcode.TEXT`, you operate `e.Data` (`WebSocketSharp.MessageEventArgs.Data`, its type is `string`).
If `e.Type` is `Opcode.TEXT`, you operate `e.Data` (`WebSocketSharp.MessageEventArgs.Data`, its type is `string`).
@ -72,21 +82,24 @@ If `e.Type` is `Opcode.BINARY`, you operate `e.RawData` (`WebSocketSharp.Message
`WebSocket.OnError` event is emitted when some error is occurred.
`WebSocket.OnError` event is emitted when some error is occurred.
```cs
ws.OnError += (sender, e) =>
ws.OnError += (sender, e) =>
{
{
...
...
};
};
```
Error message is stored in `e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, its type is `string`), so you operate it.
Error message is stored in `e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, its type is `string`), so you operate it.
##### WebSocket.OnClose event #####
##### WebSocket.OnClose event #####
`WebSocket.OnClose` event is emitted when WebSocket connection is closed.
`WebSocket.OnClose` event is emitted when WebSocket connection is closed.
```cs
ws.OnClose += (sender, e) =>
ws.OnClose += (sender, e) =>
{
{
...
...
};
};
```
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.
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.
@ -94,13 +107,17 @@ Close status code is stored in `e.Code` (`WebSocketSharp.CloseEventArgs.Code`, i
Connecting to server using WebSocket.
Connecting to server using WebSocket.
```cs
ws.Connect();
ws.Connect();
```
#### Step 5 ####
#### Step 5 ####
Sending data.
Sending data.
```cs
ws.Send(data);
ws.Send(data);
```
`WebSocket.Send` method is overloaded.
`WebSocket.Send` method is overloaded.
@ -110,7 +127,9 @@ Sending data.
Closing WebSocket connection.
Closing WebSocket connection.
```cs
ws.Close(code, reason);
ws.Close(code, reason);
```
If you want to close WebSocket connection explicitly, you can use `Close` method.
If you want to close WebSocket connection explicitly, you can use `Close` method.
@ -124,7 +143,9 @@ Type of `code` is `WebSocketSharp.Frame.CloseStatusCode`, type of `reason` is `s
Required namespace.
Required namespace.
```cs
using WebSocketSharp.Server;
using WebSocketSharp.Server;
```
`WebSocketServer<T>` class and `WebSocketService` class exist in `WebSocketSharp.Server` namespace.
`WebSocketServer<T>` class and `WebSocketService` class exist in `WebSocketSharp.Server` namespace.
@ -168,7 +189,9 @@ For example, if you want to provide the chat service,
Creating a instance of `WebSocketServer<T>` class.
Creating a instance of `WebSocketServer<T>` class.
```cs
var wssv = new WebSocketServer<Echo>("ws://example.com:4649");
var wssv = new WebSocketServer<Echo>("ws://example.com:4649");
```
Type of `T` inherits `WebSocketService` class, so you can use a class that was created in **Step 2**.
Type of `T` inherits `WebSocketService` class, so you can use a class that was created in **Step 2**.