Reputation: 1011
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.
Upvotes: 0
Views: 465
Reputation: 234
You can use C# third party dll like
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....
Upvotes: 1