somebody know what is encoding type of message that client sends to websocket server?
I’m studying this site
http://blogs.claritycon.com/blog/2012/01/18/websockets-with-rfc-6455/
and from what this site teaches me, below are decoding code to get message from client in server side!!!
public string HybiDecode(byte[] data)
{
byte firstByte = data[0];
byte secondByte = data[1];
int opcode = firstByte & 0x0F;
bool isMasked = ((firstByte & 128) == 128);
int payloadLength = secondByte & 0x7F;
if (!isMasked) { return null; } // not masked
if (opcode != 1) { return null; } // not text
List<int> mask = new List<int>();
for (int i = 2; i < 6; i++)
{
mask.Add(data[i]);
}
int payloadOffset = 6;
int dataLength = payloadLength + payloadOffset;
List<int> unmaskedPayload = new List<int>();
for (int i = payloadOffset; i < dataLength; i++)
{
int j = i - payloadOffset;
unmaskedPayload.Add(data[i] ^ mask[j % 4]);
}
return ToAscii(unmaskedPayload.Select(e => (byte)e).ToArray());
}
public string ToAscii(byte[] data)
{
System.Text.ASCIIEncoding decoder = new System.Text.ASCIIEncoding();
return decoder.GetString(data, 0, data.Length);
}
but now I’m studying in C language so I have to convert ToAscii() to C language!
but… from what? from unicode to ASCII or from utf-8 to ASCII???
could you let me know if you know about this???
Messages are transmitted as utf-8. See section 5.6 of the spec for details.
Its up to you what encoding you use within your server. UTF-8 is easy to handle in C (it is still terminated by single nul character so all string functions work) so you might find it easiest to use UTF-8 in your code and not convert to ascii/unicode.