**websocket-sharp** is a C# implementation of WebSocket protocol client & server.
**websocket-sharp** is a C# implementation of the WebSocket protocol client & server.
## Usage ##
### WebSocket Client ###
### The WebSocket client ###
#### Step 1 ####
@ -15,11 +15,11 @@ using WebSocketSharp;
using WebSocketSharp.Frame;
```
`WebSocket` class exists in `WebSocketSharp` namespace, WebSocket data frame resources (e.g. `WsFrame` class) exist in `WebSocketSharp.Frame` namespace.
The `WebSocket` class exists in the `WebSocketSharp` namespace, the WebSocket frame resources (e.g. `WsFrame` class) exist in the`WebSocketSharp.Frame` namespace.
#### Step 2 ####
Creating a instance of `WebSocket` class.
Creating a instance of the `WebSocket` class.
```cs
using (WebSocket ws = new WebSocket("ws://example.com"))
@ -28,15 +28,15 @@ using (WebSocket ws = new WebSocket("ws://example.com"))
}
```
`WebSocket` class inherits `IDisposable` interface, so you can use `using` statement.
The `WebSocket` class inherits the`IDisposable` interface, so you can use the `using` statement.
#### Step 3 ####
Setting `WebSocket` event handlers.
Setting the `WebSocket` events.
##### WebSocket.OnOpen event #####
`WebSocket.OnOpen` event is emitted immediately after WebSocket connection has been established.
The `WebSocket.OnOpen` event occurs when the WebSocket connection has been established.
```cs
ws.OnOpen += (sender, e) =>
@ -45,11 +45,11 @@ ws.OnOpen += (sender, e) =>
};
```
`e` has come across as `EventArgs.Empty`, so there is no operation on `e`.
The `e` has come across as the`EventArgs.Empty`, so there is no operation on the`e`.
##### WebSocket.OnMessage event #####
`WebSocket.OnMessage` event is emitted each time WebSocket data frame is received.
The `WebSocket.OnMessage` event occurs when the `WebSocket` receives a data frame.
```cs
ws.OnMessage += (sender, e) =>
@ -58,7 +58,7 @@ 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.
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.
```cs
switch (e.Type)
@ -74,13 +74,13 @@ switch (e.Type)
}
```
If `e.Type` is `Opcode.TEXT`, you operate `e.Data` (`WebSocketSharp.MessageEventArgs.Data`, its type is `string`).
If the `e.Type` is `Opcode.TEXT`, you operate the `e.Data` (`WebSocketSharp.MessageEventArgs.Data`, its type is `string`).
If `e.Type` is `Opcode.BINARY`, you operate `e.RawData` (`WebSocketSharp.MessageEventArgs.RawData`, its type is `byte[]`).
If the `e.Type` is `Opcode.BINARY`, you operate the `e.RawData` (`WebSocketSharp.MessageEventArgs.RawData`, its type is `byte[]`).
##### WebSocket.OnError event #####
`WebSocket.OnError` event is emitted when some error is occurred.
The `WebSocket.OnError` event occurs when the `WebSocket` gets an error.
```cs
ws.OnError += (sender, e) =>
@ -88,11 +88,11 @@ ws.OnError += (sender, e) =>
...
};
```
Error message is stored in`e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, its type is `string`), so you operate it.
The`e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, its type is `string`) contains the error message, so you operate it.
##### WebSocket.OnClose event #####
`WebSocket.OnClose` event is emitted when WebSocket connection is closed.
The `WebSocket.OnClose` event occurs when the `WebSocket` receives a Close frame or the `Close` method is called.
```cs
ws.OnClose += (sender, e) =>
@ -101,11 +101,12 @@ 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.
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.
#### Step 4 ####
Connecting to server using WebSocket.
Connecting to the WebSocket server.
```cs
ws.Connect();
@ -113,31 +114,31 @@ ws.Connect();
#### Step 5 ####
Sending data.
Sending a data.
```cs
ws.Send(data);
```
`WebSocket.Send` method is overloaded.
The `Send` method is overloaded.
`data` types are `string`, `byte[]` and`FileInfo` class.
The types of `data` are `string`, `byte[]` or`FileInfo` class.
#### Step 6 ####
Closing WebSocket connection.
Closing the WebSocket connection.
```cs
ws.Close(code, reason);
```
If you want to close WebSocket connection explicitly, you can use `Close` method.
If you want to close the WebSocket connection explicitly, you can use the `Close` method.
Type of `code` is `WebSocketSharp.Frame.CloseStatusCode`, type of `reason` is `string`.
The type of `code` is `WebSocketSharp.Frame.CloseStatusCode`, the type of `reason` is `string`.
`WebSocket.Close` method is overloaded (In addition `Close()` and `Close(code)` exist).
The `Close` method is overloaded. (In addition, the`Close()` and `Close(code)`methods exist.)
### WebSocket Server ###
### The WebSocket server ###
#### Step 1 ####
@ -147,11 +148,11 @@ Required namespace.
using WebSocketSharp.Server;
```
`WebSocketServer`, `WebSocketServer<T>` and `WebSocketService` classes exist in `WebSocketSharp.Server` namespace.
The `WebSocketServer`, `WebSocketServer<T>` and `WebSocketService` classes exist in the`WebSocketSharp.Server` namespace.
#### Step 2 ####
Creating a class that inherits `WebSocketService` class.
Creating a class that inherits the `WebSocketService` class.
For example, if you want to provide the echo service,
@ -169,7 +170,7 @@ public class Echo : WebSocketService
}
```
For example, if you want to provide the chat service,
Or if you want to provide the chat service,
```cs
using System;
@ -185,19 +186,19 @@ public class Chat : WebSocketService
}
```
If you override `onMessage` method, it is bound to server side `WebSocket.OnMessage` event.
If you override the `onMessage` method, it is bound to the server side `WebSocket.OnMessage` event.
In addition, if you override `onOpen`, `onError` and `onClose` methods, each of them is bound to `WebSocket.OnOpen`, `WebSocket.OnError` and `WebSocket.OnClose` events.
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.
#### Step 3 ####
Creating a instance of `WebSocketServer<T>` class if you want single WebSocket service server.
Creating a instance of the `WebSocketServer<T>` class if you want the single WebSocket service server.
```cs
var wssv = new WebSocketServer<Echo>("ws://example.com:4649");
```
Creating a instance of `WebSocketServer` class if you want multi WebSocket service server.
Creating a instance of the `WebSocketServer` class if you want the multi WebSocket service server.
Error message is stored in`e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, its type is `string`), so you operate it.
The`e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, its type is `string`) contains the error message, so you operate it.
##### WebSocketServer.OnError event #####
Same as `WebSocketServer<T>.OnError` event.
Same as the `WebSocketServer<T>.OnError` event.
#### Step 5 ####
Starting server.
Starting the server.
```cs
wssv.Start();
@ -245,17 +246,17 @@ wssv.Start();
#### Step 6 ####
Stopping server.
Stopping the server.
```cs
wssv.Stop();
```
### HTTP Server with WebSocket ###
### The HTTP server with the WebSocket ###
I modified System.Net.HttpListener, System.Net.HttpListenerContext and some others of [Mono] to create the HTTP server that the connection can be upgraded to the WebSocket connection if the HTTP server received a WebSocket request.
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.
You can add to your `HttpServer` any WebSocket service and a matching path to that service by using `HttpServer.AddService<T>` method.
You can add to your `HttpServer` any WebSocket service and a matching path to that service by using the `HttpServer.AddService<T>` method.
```cs
var httpsv = new HttpServer(4649);
@ -284,7 +285,7 @@ Examples of using **websocket-sharp**.
### Example3 ###
[Example3] starts the HTTP server that the connection can be upgraded to the WebSocket connection.
[Example3] starts the HTTP server that can upgrade the connection to the WebSocket connection.
Please access [http://localhost:4649](http://localhost:4649) to do WebSocket Echo Test with your web browser after [Example3] running.
@ -326,7 +327,7 @@ Licensed under the **[MIT License]**.
/// Initializes a new instance of the <see cref="WebSocketSharp.WebSocket"/> class with the specified WebSocket URL, OnOpen, OnMessage, OnError, OnClose event handlers and subprotocols.
/// </summary>
/// <param name='url'>
/// A <see cref="string"/> that contains the WebSocket URL.
/// </param>
/// <param name='onOpen'>
/// An OnOpen event handler.
/// </param>
/// <param name='onMessage'>
/// An OnMessage event handler.
/// </param>
/// <param name='onError'>
/// An OnError event handler.
/// </param>
/// <param name='onClose'>
/// An OnClose event handler.
/// </param>
/// <param name='protocols'>
/// An array of <see cref="string"/> that contains the WebSocket subprotocols if any.
/// </param>
/// <exception cref='ArgumentException'>
/// <paramref name="url"/> is not valid WebSocket URL.
/// </exception>
publicWebSocket(
stringurl,
EventHandleronOpen,
@ -172,28 +212,43 @@ namespace WebSocketSharp {
#region Properties
publicstringBinaryType{
get{return_binaryType;}
}
/// <summary>
/// Gets the amount of untransmitted data.
/// </summary>
/// <value>
/// The number of bytes of untransmitted data.
/// </value>
publiculongBufferedAmount{
get{
ulongbufferedAmount=0;
lock(_unTransmittedBuffer.SyncRoot)
{
ulongbufferedAmount=0;
foreach(WsFrameframein_unTransmittedBuffer)
bufferedAmount+=frame.PayloadLength;
}
returnbufferedAmount;
returnbufferedAmount;
}
}
}
/// <summary>
/// Gets the extensions selected by the server.
/// </summary>
/// <value>
/// A <see cref="string"/> that contains the extensions if any. By default, <c>String.Empty</c>. (Currently this will only ever be the <c>String.Empty</c>.)
/// </value>
publicstringExtensions{
get{return_extensions;}
get{
return_extensions;
}
}
/// <summary>
/// Gets a value indicating whether a connection is alive.
/// </summary>
/// <value>
/// <c>true</c> if the connection is alive; otherwise, <c>false</c>.
/// </value>
publicboolIsAlive{
get{
if(_readyState!=WsState.OPEN)
@ -203,18 +258,40 @@ namespace WebSocketSharp {
}
}
/// <summary>
/// Gets a value indicating whether a connection is secure.
/// </summary>
/// <value>
/// <c>true</c> if the connection is secure; otherwise, <c>false</c>.
/// </value>
publicboolIsSecure{
get{
return_isSecure;
}
}
/// <summary>
/// Gets the subprotocol selected by the server.
/// </summary>
/// <value>
/// A <see cref="string"/> that contains the subprotocol if any. By default, <c>String.Empty</c>.
/// </value>
publicstringProtocol{
get{return_protocol;}
get{
return_protocol;
}
}
/// <summary>
/// Gets the state of the connection.
/// </summary>
/// <value>
/// A <see cref="WebSocketSharp.WsState"/>. By default, <c>WsState.CONNECTING</c>.