Added WebSocketServer
This commit is contained in:
129
README.md
129
README.md
@@ -1,10 +1,12 @@
|
||||
# websocket-sharp #
|
||||
|
||||
**websocket-sharp** is a C# implementation of a WebSocket protocol client.
|
||||
**websocket-sharp** is a C# implementation of a WebSocket protocol client & server.
|
||||
|
||||
## Usage ##
|
||||
|
||||
### Step 1 ###
|
||||
### WebSocket Client ###
|
||||
|
||||
#### Step 1 ####
|
||||
|
||||
Required namespaces.
|
||||
|
||||
@@ -13,7 +15,7 @@ Required namespaces.
|
||||
|
||||
In `WebSocketSharp` namespace `WebSocket` class exists, in `WebSocketSharp.Frame` namespace WebSocket data frame resources (e.g. `WsFrame` class) exist.
|
||||
|
||||
### Step 2 ###
|
||||
#### Step 2 ####
|
||||
|
||||
Creating instance of `WebSocket` class.
|
||||
|
||||
@@ -22,13 +24,13 @@ Creating instance of `WebSocket` class.
|
||||
...
|
||||
}
|
||||
|
||||
So `WebSocket` class inherits `IDisposable` interface, you can use `using` statement.
|
||||
`WebSocket` class inherits `IDisposable` interface, so you can use `using` statement.
|
||||
|
||||
### Step 3 ###
|
||||
#### Step 3 ####
|
||||
|
||||
Setting `WebSocket` event handlers.
|
||||
|
||||
#### WebSocket.OnOpen event ####
|
||||
##### WebSocket.OnOpen event #####
|
||||
|
||||
`WebSocket.OnOpen` event is emitted immediately after WebSocket connection has been established.
|
||||
|
||||
@@ -37,9 +39,9 @@ Setting `WebSocket` event handlers.
|
||||
...
|
||||
};
|
||||
|
||||
So `e` has come across as `EventArgs.Empty`, there is no operation on `e`.
|
||||
`e` has come across as `EventArgs.Empty`, so there is no operation on `e`.
|
||||
|
||||
#### WebSocket.OnMessage event ####
|
||||
##### WebSocket.OnMessage event #####
|
||||
|
||||
`WebSocket.OnMessage` event is emitted each time WebSocket data frame is received.
|
||||
|
||||
@@ -48,7 +50,7 @@ So `e` has come across as `EventArgs.Empty`, there is no operation on `e`.
|
||||
...
|
||||
};
|
||||
|
||||
So **type** of received WebSocket data frame is stored in `e.Type` (`WebSocketSharp.MessageEventArgs.Type`, its type is `WebSocketSharp.Frame.Opcode`), you check it out and you determine which item you should operate.
|
||||
**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.
|
||||
|
||||
switch (e.Type)
|
||||
{
|
||||
@@ -66,7 +68,7 @@ If `e.Type` is `Opcode.TEXT`, you operate `e.Data` (`WebSocketSharp.MessageEvent
|
||||
|
||||
If `e.Type` is `Opcode.BINARY`, you operate `e.RawData` (`WebSocketSharp.MessageEventArgs.RawData`, its type is `byte[]`).
|
||||
|
||||
#### WebSocket.OnError event ####
|
||||
##### WebSocket.OnError event #####
|
||||
|
||||
`WebSocket.OnError` event is emitted when some error is occurred.
|
||||
|
||||
@@ -75,9 +77,9 @@ If `e.Type` is `Opcode.BINARY`, you operate `e.RawData` (`WebSocketSharp.Message
|
||||
...
|
||||
};
|
||||
|
||||
So error message is stored in `e.Data` (`WebSocketSharp.MessageEventArgs.Data`, its type is `string`), 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.
|
||||
|
||||
@@ -86,15 +88,15 @@ So error message is stored in `e.Data` (`WebSocketSharp.MessageEventArgs.Data`,
|
||||
...
|
||||
};
|
||||
|
||||
So 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`), 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.
|
||||
|
||||
### Step 4 ###
|
||||
#### Step 4 ####
|
||||
|
||||
Connecting to server using WebSocket.
|
||||
|
||||
ws.Connect();
|
||||
|
||||
### Step 5 ###
|
||||
#### Step 5 ####
|
||||
|
||||
Sending data.
|
||||
|
||||
@@ -104,7 +106,7 @@ Sending data.
|
||||
|
||||
`data` types are `string`, `byte[]` and `FileInfo` class.
|
||||
|
||||
### Step 6 ###
|
||||
#### Step 6 ####
|
||||
|
||||
Closing WebSocket connection.
|
||||
|
||||
@@ -116,6 +118,96 @@ Type of `code` is `WebSocketSharp.Frame.CloseStatusCode`, type of `reason` is `s
|
||||
|
||||
`WebSocket.Close` method is overloaded (In addition `Close()` and `Close(code)` exist).
|
||||
|
||||
### WebSocket Server ###
|
||||
|
||||
#### Step 1 ####
|
||||
|
||||
Required namespaces.
|
||||
|
||||
using WebSocketSharp;
|
||||
using WebSocketSharp.Frame;
|
||||
|
||||
Same as **WebSocket Client**.
|
||||
|
||||
#### Step 2 ####
|
||||
|
||||
Creating instance of `WebSocketServer` class.
|
||||
|
||||
WebSocketServer wssv = new WebSocketServer("ws://example.com:4649");
|
||||
|
||||
If you set WebSocket url without port number, `WebSocketServer` set 80 or 443 to port number automatically.
|
||||
So it is necessary to run with root permission.
|
||||
|
||||
$ sudo mono example2.exe
|
||||
|
||||
#### Step 3 ####
|
||||
|
||||
Setting WebSocketServer event handlers.
|
||||
|
||||
##### WebSocketServer.OnConnection event #####
|
||||
|
||||
`WebSocketServer.OnConnection` event is emitted each time client makes a connection request.
|
||||
|
||||
wssv.OnConnection += (sender, e) =>
|
||||
{
|
||||
...
|
||||
};
|
||||
|
||||
`WebSocket` to communicate with client is stored in `e.Socket` (`WebSocketSharp.ConnectionEventArgs.Socket`, its type is `WebSocketSharp.WebSocket`), so you operate it.
|
||||
|
||||
WebSocket ws = e.Socket;
|
||||
ws.OnMessage += (sender_, e_) =>
|
||||
{
|
||||
...
|
||||
};
|
||||
|
||||
Same settings of `WebSocket` event handlers as **WebSocket Client**.
|
||||
|
||||
This WebSocket is server-side, so data is sent from server to client.
|
||||
|
||||
If you want to function as echo server that returns a data to client as it is received,
|
||||
|
||||
// Echo
|
||||
ws.Send(e_.Data);
|
||||
|
||||
If you want to function as chat server that returns a data to all clients,
|
||||
|
||||
// Chat
|
||||
wssv.Send(e_.Data);
|
||||
|
||||
##### 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.
|
||||
|
||||
#### Step 4 ####
|
||||
|
||||
Starting server.
|
||||
|
||||
wssv.Start();
|
||||
|
||||
#### Step 5 ####
|
||||
|
||||
Sending data to all clients.
|
||||
|
||||
wssv.Send(data);
|
||||
|
||||
`WebSocketServer.Send` method is overloaded.
|
||||
|
||||
`data` types are `string` and `byte[]`.
|
||||
|
||||
#### Step 6 ####
|
||||
|
||||
Stopping server.
|
||||
|
||||
wssv.Stop();
|
||||
|
||||
## Examples ##
|
||||
|
||||
Examples of using **websocket-sharp**.
|
||||
@@ -130,6 +222,10 @@ Examples of using **websocket-sharp**.
|
||||
|
||||
[Example1] uses [Json.NET].
|
||||
|
||||
### Example2 ###
|
||||
|
||||
[Example2] starts WebSocket server.
|
||||
|
||||
## Supported WebSocket Protocol ##
|
||||
|
||||
**websocket-sharp** supports **[RFC 6455]**.
|
||||
@@ -160,6 +256,7 @@ Licensed under the **[MIT License]**.
|
||||
[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
|
||||
[Example2]: https://github.com/sta/websocket-sharp/tree/master/Example2
|
||||
[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
|
||||
|
||||
Reference in New Issue
Block a user