現在我正在處理Websockets,我是新的,我終於可以發送126字節的消息,但我需要發送更長的消息,但是當我嘗試yhe連接時自動關閉,我的代碼是:在C#中發送比126字節的websockets更大的消息
public void sendMessage(Stream stream, string message)
{
try
{
List<byte> lb = new List<byte>();
string aux = message;
bool flagStart = false;
int size;
while (message.Length > _maxLengthMessage)
{
lb = new List<byte>();
// I cut the mesasge in smaller pieces to send
message = aux.Substring(0, _maxLengthMessage);
aux = aux.Substring(_maxLengthMessage);
if (!flagStart)
{
// In doc of Websockets i sign this piece: not the end, text
lb.Add(0x01);
flagStart = !flagStart;
}
else
{
// In doc of Websockets i sign this piece: not the end, continuation
lb.Add(0x00);
}
size = message.Length;
lb.Add((byte)size);
lb.AddRange(Encoding.UTF8.GetBytes(message));
stream.Write(lb.ToArray(), 0, size + 2);
}
lb = new List<byte>();
if (!flagStart)
{
// If is this the only message we mark with: end of message, text
lb.Add(0x81);
flagStart = !flagStart;
}
else
{
//else Is the end of the message but is the continuation frame
lb.Add(0x80);
}
size = aux.Length;
lb.Add((byte)size);
lb.AddRange(Encoding.UTF8.GetBytes(aux));
//lb.AddRange(Encoding.UTF8.GetBytes(i.ToString()));
stream.Write(lb.ToArray(), 0, size+2);
}
catch (Exception ex)
{
throw ex;
}
}
我不知道如果我做得很好,但我需要幫助,這是迫切的,我不知道該怎麼做,我搜索互聯網,但我對總是找到相同的答案「轉到WebSocket協議」我做到了,沒有任何事,也許我不是很瞭解,所以我需要你的幫助。
謝謝。
附子嗨,你能不能把你的更正後的代碼? – Novalis 2012-09-12 11:18:11