`WebSocket` class exists in `WebSocketSharp` namespace, WebSocket data frame resources (e.g. `WsFrame` class) exist in `WebSocketSharp.Frame` namespace.
**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.
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.
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.