HTML and CSS Reference
In-Depth Information
ReadMessage,
null);
}
protected void OnMessageReceived(string msg)
{
// When a message is received, call the event handler if
// one has been specified
if (MessageReceived != null)
MessageReceived(this, new MessageReceivedEventArgs(msg, msg.
Length));
}
public void Dispose()
{
_mySocket.Close();
}
}
}
The WsConnection class has three class members:
_mySocket - The Socket object created for this connection. This is instantiated by the
WsServer class and passed into the constructor.
_inputBuffer - This is a byte array that holds the raw frame data. This is populated by the
Socket object.
_inputString - This is a StringBuilder object that contains the incoming message after
it has been processed.
The WsConnection class supports two events to notify when data has been received and when the
connection with the client has been closed. The MessageReceived event uses a MessageReceivedEventArgs
class to provide the received message to the event handler. The WsConnection class implements the Dispose()
method, which simply closes the Socket associated with this connection.
The WsConnection class has two primary methods, which you'll implement now. These methods implement
the WebSocket frame protocol.
ReadMessage()
SendMessage()
8.
Add the ReadMessage() method using the code shown in Listing 13-5
Listing 13-5. Implementing the ReadMessage() method.
protected void ReadMessage(IAsyncResult msg)
{
int sizeOfReceivedData=_mySocket.EndReceive(msg);
if (sizeOfReceivedData>0)
{
// Get the data provided in the first 2 bytes
bool final=(_inputBuffer[0] & 0x80)>0 ? true : false;
bool masked=(_inputBuffer[1] & 0x80)>0 ? true : false;
 
Search WWH ::




Custom Search