我正在用C#開發一個WebSocket服務器,並且我注意到使用send()方法來自瀏覽器(在這種情況下爲Chrome)的所有消息最多爲126個字符長度。 當我想發送大於126個字符的郵件時,它總是會發生,看起來協議會切割大於126個字符的郵件,並只傳輸前126個字符。 我試圖檢查協議定義,但沒有找到任何答案。如何通過WebSocket發送更大的消息?
所以,我的問題是,我可以通過WebSockets發送更大的消息嗎?
UPDATE: 這是我在我的C#的WebSocket服務器解析從客戶端(瀏覽器)的消息的方式:
private void ReceiveCallback(IAsyncResult _result)
{
lock (lckRead)
{
string message = string.Empty;
int startIndex = 2;
Int64 dataLength = (byte)(buffer[1] & 0x7F); // when the message is larger then 126 chars it cuts here and all i get is the first 126 chars
if (dataLength > 0)
{
if (dataLength == 126)
{
BitConverter.ToInt16(buffer, startIndex);
startIndex = 4;
}
else if (dataLength == 127)
{
BitConverter.ToInt64(buffer, startIndex);
startIndex = 10;
}
bool masked = Convert.ToBoolean((buffer[1] & 0x80) >> 7);
int maskKey = 0;
if (masked)
{
maskKey = BitConverter.ToInt32(buffer, startIndex);
startIndex = startIndex + 4;
}
byte[] payload = new byte[dataLength];
Array.Copy(buffer, (int)startIndex, payload, 0, (int)dataLength);
if (masked)
{
payload = MaskBytes(payload, maskKey);
message = Encoding.UTF8.GetString(payload);
OnDataReceived(new DataReceivedEventArgs(message.Length, message));
}
HandleMessage(message); //'message' - the message that received
Listen();
}
else
{
if (ClientDisconnected != null)
ClientDisconnected(this, EventArgs.Empty);
}
}
}
我還是不明白我怎麼能得到更大的消息,它可能與操作碼有關,但我不知道如何改變才能使其正常工作?
我在我的問題中寫道,我曾經去過那裏,沒有找到任何答案,請問您能更具體嗎? – udidu
[5.4節](http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17#section-5.4) – dtb
你能幫助我嗎?任何能夠幫助我接收大容量消息的僞代碼都會很感激.. – udidu