关于HTML5与WEBSOCKET通讯的问题C#

youyuan1980 2013-01-17 03:46:09
我在网上找了一个例子。
前面的握手能成功,发送也可以成功,但是发送的时候如果长度太大就无法发送。
想问一下是为什么?感觉他是在原来的前面加了报头不这是什么,请高手指导一下,谢谢。
sc.Send(PackData(msg));


/// <summary>
/// 打包服务器数据
/// </summary>
/// <param name="message">数据</param>
/// <returns>数据包</returns>
private static byte[] PackData(string message)
{
byte[] contentBytes = null;
byte[] temp = Encoding.UTF8.GetBytes(message);

if (temp.Length < 126)
{
contentBytes = new byte[temp.Length + 2];
contentBytes[0] = 0x81;
contentBytes[1] = (byte)temp.Length;
Array.Copy(temp, 0, contentBytes, 2, temp.Length);
}
else if (temp.Length < 0xFFFF)
{
contentBytes = new byte[temp.Length + 4];
contentBytes[0] = 0x81;
contentBytes[1] = 126;
contentBytes[2] = (byte)(temp.Length & 0xFF);
contentBytes[3] = (byte)(temp.Length >> 8 & 0xFF);
Array.Copy(temp, 0, contentBytes, 4, temp.Length);
}
else
{
// 暂不处理超长内容
}

return contentBytes;
}
...全文
117 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
本拉灯 2013-01-17
  • 打赏
  • 举报
回复
http://www.oursnet.net/ 这个就有可以发大容量的WebSocket了
本拉灯 2013-01-17
  • 打赏
  • 举报
回复
引用 楼主 youyuan1980 的回复:
我在网上找了一个例子。 前面的握手能成功,发送也可以成功,但是发送的时候如果长度太大就无法发送。 想问一下是为什么?感觉他是在原来的前面加了报头不这是什么,请高手指导一下,谢谢。 sc.Send(PackData(msg)); C# code?1234567891011121314151617181920212223242526272829……
WEBSOCKET 协议是根据包的长度,而设定不同的包头的
lugang386 2013-01-17
  • 打赏
  • 举报
回复
貌似,<126 时 ,数据包后面加两位校验位,,<0xFFFF 时 ,数据包后面加4位校验位,用途应该是用来判读数据的准确性完整性的
youyuan1980 2013-01-17
  • 打赏
  • 举报
回复
试了你的方式,不行。。。。。。那我还不如直接返回temp呢,何必要拷贝呢?
lugang386 2013-01-17
  • 打赏
  • 举报
回复
这个规则应该在你们程序通讯协议里自己规定的,你若不知道协议,你可以这样测试一下,看看对付解析会不会成功 /// <summary> /// 打包服务器数据 /// </summary> /// <param name="message">数据</param> /// <returns>数据包</returns> private static byte[] PackData(string message) { byte[] contentBytes = null; byte[] temp = Encoding.UTF8.GetBytes(message); if (temp.Length < 126) { contentBytes = new byte[temp.Length + 2]; contentBytes[0] = 0x81; contentBytes[1] = (byte)temp.Length; Array.Copy(temp, 0, contentBytes, 2, temp.Length); } else if (temp.Length < 0xFFFF) { contentBytes = new byte[temp.Length + 4]; contentBytes[0] = 0x81; contentBytes[1] = 126; contentBytes[2] = (byte)(temp.Length & 0xFF); contentBytes[3] = (byte)(temp.Length >> 8 & 0xFF); Array.Copy(temp, 0, contentBytes, 4, temp.Length); } else { contentBytes = temp; } return contentBytes; }
youyuan1980 2013-01-17
  • 打赏
  • 举报
回复
我知道写了。就是想问一下,为什么要加这些信息。。。有什么规则吗?
lugang386 2013-01-17
  • 打赏
  • 举报
回复
代码中不是写了嘛,<126,< 0xFFFF, 暂不处理超长内容 >0xFFFF 时contentBytes= null了

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧