What is the type of encoding when the client sends a message to the websocket server?

does anyone know what is the type of message encoding that the client sends to the websocket server?

I am researching this site


and from what this site teaches me, below is the decode code to receive a message from a client on the 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++)
    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 am learning in C language, so I need to convert ToAscii () to C language! but ... from what? unicode to ASCII or utf-8 to ASCII ??? could you please let me know if you know about this?


source to share

1 answer

Messages are transmitted as utf-8. See section 5.6 of the specification for details .

It is up to you what encoding you are using on your server. UTF-8 is easy to handle in C (it still ends with one null character, so all string functions work), so you might find it easiest to use UTF-8 in your code, rather than convert to ascii / unicode.



All Articles