5 using System.Collections.Generic;
6 using System.Security.Cryptography;
16 private static string WebsocketHeaderAcceptKey =
"Sec-WebSocket-Accept";
17 private static string WebsocketHeaderKey =
"Sec-WebSocket-Key";
18 private static string WebsocketProtocolKey =
"Sec-WebSocket-Protocol";
19 private static string WebsocketProtocolVersionKey =
"Sec-WebSocket-Version";
20 private static string GlobalUniqueIdentifier =
"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
21 private static SHA1 sha1Provider = SHA1.Create();
22 private enum SocketState
31 SocketState state = SocketState.Initial;
32 Queue<WebsocketMessage> messageQueue =
new Queue<WebsocketMessage>();
33 List<IWebsocketModule> websocketModules =
new List<IWebsocketModule>();
34 string expectedguid =
string.Empty;
37 public string WebsocketURI {
get;
set; }
45 #region Public Methods 53 websocketModules.Add(websocketModule);
62 messageQueue.Enqueue(message);
65 #region Handler Implementation 71 bool IHandler.HandleReceivedMessage(
Client client,
Message message)
73 if(state == SocketState.Handshaking)
77 if(response.StatusCode ==
HttpMessage.StatusCodes.SwitchingProtocols &&
78 response[
"Upgrade"] ==
"websocket" &&
79 response[
HttpMessage.GeneralHeadersEnum.Connection].ToLower() ==
"upgrade" &&
80 response[WebsocketHeaderAcceptKey].Trim() == expectedguid)
82 state = SocketState.Established;
90 else if(state == SocketState.Established)
115 bool IHandler.HandleSendMessage(Client client, ref Message message)
120 bool IHandler.InitializeConnection(Client client)
122 state = SocketState.Handshaking;
123 string handshakeGuid = Guid.NewGuid().ToString();
124 string handshakeKey = handshakeGuid+ GlobalUniqueIdentifier;
125 expectedguid = Convert.ToBase64String(sha1Provider.ComputeHash(Encoding.UTF8.GetBytes(handshakeKey)));
128 beginRequest.RequestURI = WebsocketURI;
129 beginRequest[
HttpMessage.RequestHeadersEnum.Host] = client.Address;
130 beginRequest[
HttpMessage.GeneralHeadersEnum.Connection] =
"Upgrade";
131 beginRequest[WebsocketHeaderKey] = handshakeGuid;
132 beginRequest[
"Origin"] = client.Address;
133 beginRequest[WebsocketProtocolKey] =
"chat";
134 beginRequest[WebsocketProtocolVersionKey] =
"13";
139 bool IHandler.PreHandleReceivedMessage(Client client, ref Message message)
144 void IHandler.Tick(Client client)
146 while(messageQueue.Count > 0)
bool HandleReceivedMessage(Client.Client client, WebsocketHandler handler, WebsocketMessage messageBytes)
Handle received message.
void QueueMessage(WebsocketMessage message)
Add a message to the queue to be sent.
void ConnectionClosed(Client.Client client, WebsocketHandler handler)
Called when an existing websocket connection is being closed.
void ConnectionInitialized(Client.Client client, WebsocketHandler handler)
Called when a new websocket connection has been initialized.
WebsocketHandler AddModule(IWebsocketModule websocketModule)
Add a module to the websocket handler.
Interface for websocket modules.
Class containing the bytes of a websocket received message.
byte [] BuildRequest()
Build the request bytes based on the message contents.
void Disconnected(Client client)
Called when the client is disconnected.
byte [] GetMessageBytes()
Get the packed message bytes.
Handler implementing websocket protocol.
Handler interface for client communication.
void ParseResponse(byte[] responseBytes)
Parse the received bytes and populate the message contents.
Base class for TCP Client.
Opcodes Opcode
Frame Opcode