WebSocket发送消息,大于126字节和大于65535字节的处理
2015-10-28 17:01
573 查看
网上搜了一下WebSocket的合包函数,发现不能直接使用。查了很多资料,终于弄出来了,大于65535字节的没有测试,但是小于65535的测试通过下面是C#代码,其他代码根据此代码修改即可public static byte[] PackData(string message) { byte[] contentBytes = null; byte[] temp = Encoding.UTF8.GetBytes(message); LogWrite("内容长度:" + temp.Length); 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 >>8); contentBytes[3] = (byte)(temp.Length & 0xFF); Array.Copy(temp, 0, contentBytes, 4, temp.Length); } else { contentBytes = new byte[temp.Length + 10]; contentBytes[0] = 0x81; contentBytes[1] = 127; contentBytes[2] = 0; contentBytes[3] = 0; contentBytes[4] = 0; contentBytes[5] = 0; contentBytes[6] = (byte)(temp.Length >>24); contentBytes[7] = (byte)(temp.Length >>16); contentBytes[8] = (byte)(temp.Length >>8); contentBytes[9] = (byte)(temp.Length & 0xFF); Array.Copy(temp, 0, contentBytes, 10, temp.Length); }
return contentBytes; }
return contentBytes; }
相关文章推荐
- C# ping操作,MSDN版(不是socket自己写值的版本)
- Ubuntu无法安装rpm包,ubuntu RPM should not be used directly install RPM packages, use Alien instead!
- FireBug,Chrome调试工具,动态加载jQuery库或其他库代码
- css3在不同型号手机浏览器上的兼容一览表
- 转:面试题:“你能不能谈谈,java GC是在什么时候,对什么东西,做了什么事情?”
- 批量检测服务器是否可以使用ssh登陆
- 屏幕适配的选择
- crontab定时器不执行
- selenium 常用总结
- MP4文件格式的解析,以及MP4文件的分割算法
- Android如何判断一个应用在运行
- Ubuntu安装系统状态监视器Indicator-Multiload
- 项目收尾、知识产权管理与法律法规、标准规范
- stringByAddingPercentEscapesUsingEncoding
- 连接数据源Datasource与DSO
- 用linux远程访问linux
- linux 安装jdk1.6
- centos6.4x64安装php遇到的错误
- CentOS-6.4安装MySQL 5.6.10并修改MySQL的root用户密码
- Linux服务器下Can\'t connect to MySQL server on \'\' (10060)