Ive made a Websocket server in C# and a HTML UI together with JS.
I can get the server to communicate and do the handshake properly, but the information being sent is absolute gibberish and looks something like this:
???=u??X?G?
I have tried encoding it, but the result is not very different.
My JS looks like this:
// the user clicked the big red button
$('#bigredbutton_send').click(function () {
ws_send($('#console_input').val());
});
$('#console_input').keyup(function (e) {
if(e.keyCode == 13) // enter is pressed
ws_send($('#console_input').val());
});
CSS:
<input type="image" src="button.png" name="bigredbutton_send" id="bigredbutton_send" value="VALUE" />
<input type="text" name="console_input" id="console_input" value="Hello123" />
and c# module that recieves the information looks like this:
private void Read(IAsyncResult ar)
{
int sizeOfReceivedData = ConnectionSocket.EndReceive(ar);
if (sizeOfReceivedData > 0)
{
int start = 0, end = dataBuffer.Length - 1;
// if we are not already reading something, look for the start byte as specified in the protocol
if (!readingData)
{
for (start = 0; start < dataBuffer.Length - 1; start++)
{
if (dataBuffer[start] == (byte)WrapperBytes.Start)
{
readingData = true; // we found the begining and can now start reading
start++; // we dont need the start byte. Incrementing the start counter will walk us past it
break;
}
}
} // no else here, the value of readingData might have changed
// if a begining was found in the buffer, or if we are continuing from another buffer
if (readingData)
{
bool endIsInThisBuffer = false;
// look for the end byte in the received data
for (end = start; end < sizeOfReceivedData; end++)
{
byte currentByte = dataBuffer[end];
if (dataBuffer[end] == (byte)WrapperBytes.End)
{
endIsInThisBuffer = true; // we found the ending byte
break;
}
}
// the end is in this buffer, which means that we are done reading
if (endIsInThisBuffer == true)
{
// we are no longer reading data
readingData = false;
// put the data into the string builder
dataString.Append(Encoding.UTF8.GetString(dataBuffer, start, end - start));
// trigger the event
int size = Encoding.UTF8.GetBytes(dataString.ToString().ToCharArray()).Length;
recievedData = dataString.ToString();
OnDataReceived(new DataReceivedEventArgs(size, dataString.ToString()));
dataString = null;
dataString = new StringBuilder();
}
else // if the end is not in this buffer then put everyting from start to the end of the buffer into the datastring and keep on reading
{
dataString.Append(Encoding.UTF8.GetString(dataBuffer, start, end - start));
}
}
// continue listening for more data
Listen();
}
else // the socket is closed
{
if (Disconnected != null)
Disconnected(this, EventArgs.Empty);
}
// Testing to see if readable
ReadRecievedData(Convert.ToString(dataString));
}
And they all return something, however, they always return this raw, gibberishlike, data that looks like this:
???=u??X?G?
I understand that it lacks encoding, and I have tried to encode it several times - but the information just looks weirder and actually never returns anything of what I want it to. Any help would be greatly appreciated.
Update
The dataBuffer is called when a new connection is invoked.
public WebSocketConnection(Socket socket, int bufferSize)
{
ConnectionSocket = socket;
dataBuffer = new byte[bufferSize];
dataString = new StringBuilder();
GUID = System.Guid.NewGuid();
Listen();
}
Listen() creates this:
private void Listen()
{
ConnectionSocket.BeginReceive(dataBuffer, 0, dataBuffer.Length, 0, Read, null);
}
SOLVED!
I looked over it again yesterday and I solved the problem. I wasnt parsing the bits correct - So I created this instead:
byte b = dataBuffer[1];
int dataLength = 0;
int totalLength = 0;
int keyIndex = 0;
int length = dataBuffer.Length;
if (b - 128 <= 125)
{
dataLength = b - 128;
keyIndex = 2;
totalLength = dataLength + 6;
}
if (b - 128 == 126)
{
dataLength = BitConverter.ToInt16(new byte[] { dataBuffer[3], dataBuffer[2] }, 0);
keyIndex = 4;
totalLength = dataLength + 8;
}
if (b - 128 == 127)
{
dataLength = (int)BitConverter.ToInt64(new byte[] { dataBuffer[9], dataBuffer[8], dataBuffer[7], dataBuffer[6], dataBuffer[5], dataBuffer[4],
dataBuffer[3], dataBuffer[2] }, 0);
keyIndex = 10;
totalLength = dataLength + 14;
}
if (totalLength > length)
throw new Exception("The buffer length is small than the data length");
byte[] key = new byte[] { dataBuffer[keyIndex], dataBuffer[keyIndex + 1], dataBuffer[keyIndex + 2], dataBuffer[keyIndex + 3] };
int dataIndex = keyIndex + 4;
int count = 0;
for (int i = dataIndex; i < totalLength; i++)
{
dataBuffer[i] = (byte)(dataBuffer[i] ^ key[count % 4]);
count++;
}
ReadRecievedData(Encoding.ASCII.GetString(dataBuffer, dataIndex, dataLength));
It builds on the solution over hereHow to (de)construct data frames in WebSockets hybi 08+?