0

I'm implemeting a websocket server in C# and client in JS for my college and i'm having trouble setting it up. I can connect, handshake and all correctly and tested. But after that, I could not read the messages coming from the browser, until I found this function here on stackoverflow (I lost the link, sorry):

private static string byteToString(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;

    if (opcode != 1)
        return null;

    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]);
    }

    byte[] data2=unmaskedPayload.Select(e => (byte)e).ToArray();
    System.Text.ASCIIEncoding decoder = new System.Text.ASCIIEncoding();
    string result = decoder.GetString(data2, 0, data2.Length);

    return result;
}

It is supposed to get the data[] from the buffer, and magically turn into a readable string, and it works like a charm, but if I input a large string via send() in the browser, the function doesn't work at all. Unfortunatelly I have no idea how this function works to find the problem, do you guys have any clues of this behavior? I`m happy to provide any code i can.

Thanks in advance.

2
  • You just need to convert your byte[] to String - stackoverflow.com/questions/1003275/… Commented Jan 30, 2015 at 7:38
  • Already tried that way, it just spits random chars :( Commented Jan 30, 2015 at 18:34

1 Answer 1

1

You can use C# third party dll like

Alchemy dll

Superwebsocket dll

By using this dll, we can connect more than 100 connections concurrently and we can communicate with all client separately like real time data feed. This also handle threading inside it....

Sign up to request clarification or add additional context in comments.

1 Comment

That doesn't answer my question, but its a welcome suggestion! Already tested and love it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.