HTML and CSS Reference
In-Depth Information
// Copy the data into the input string and empty the buffer
.GetString(payload, 0, (int)actualLength));
Array.Clear(_inputBuffer, 0, _inputBuffer.Length);
// If this is the final frame, raise an event and clear the input
if (final)
// Do something with the data
// Clear the input string
// Listening for more messages
// If we were not able to read the message, assume that
// the socket is closed
if (Disconnected != null)
Disconnected(this, EventArgs.Empty);
ReadMessage() processes a single incoming frame. It looks at the first two bytes to determine where the
length is specified and if masking is used. It then gets the actual length and extracts the mask. Finally the data
is unmasked. The raw data for the frame is placed in the _inputBuffer byte array by the Socket object. The
processed data is stored in the _inputString member. Both of these are class members. The processed data from
each frame is appended to the _inputString member. When the final frame has been processed, the entire string
is passed to the OnMessageReceived() method. This allows for a single message to be transmitted in multiple
frames. The OnMessageReceived() method simply invokes the event handler, if defined.
Add the SendMessage() method using the code in Listing 13-6.
Listing 13-6. Implementing the SendMessage() method
public void SendMessage(string msg)
if (_mySocket.Connected)
// Create the output buffer
Int64 dataLength=msg.Length;
int dataStart=0;
byte[] dataOut=new byte[dataLength+10];
Search WWH ::

Custom Search