- #region 打包请求连接数据
- /// <summary>
- /// 打包请求连接数据
- /// </summary>
- /// <param name="handShakeBytes"></param>
- /// <param name="length"></param>
- /// <returns></returns>
- private byte[] PackageHandShakeData(byte[] handShakeBytes, int length) {
- string handShakeText = Encoding.UTF8.GetString(handShakeBytes, 0, length);
- string key = string.Empty;
- Regex reg = new Regex(@"Sec\-webSocket\-Key:(.*?)\r\n");
- Match m = reg.Match(handShakeText);
- if (m.Value != "") {
- key = Regex.Replace(m.Value, @"Sec\-WebSocket\-Key:(.*?)\r\n", "$1").Trim();
- }
- byte[] secKeyBytes = SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));
- string secKey = Convert.ToBase64String(secKeyBytes);
- var responseBuilder = new StringBuilder();
- responseBuilder.Append("HTTP/1.1 101 Switching Protocols" + "\r\n");
- responseBuilder.Append("Upgrade: websocket" + "\r\n");
- responseBuilder.Append("Connection: Upgrade" + "\r\n");
- responseBuilder.Append("Sec-WebSocket-Accept:" + secKey + "\r\n\r\n");
- return Encoding.UTF8.GetBytes(responseBuilder.ToString());
- }
- #endregion
- #region 处理接收的数据
- /// <summary>
- /// 处理接收的数据
- /// 参考 http://www.cnblogs.com/smark/archive/2012/11/26/2789812.html
- /// </summary>
- /// <param name="recBytes"></param>
- /// <param name="length"></param>
- /// <returns></returns>
- private string AnalyzeClientData(byte[] recBytes, int length) {
- int start = 0;
- // 如果有数据则至少包括 3 位
- if (length <2) return "";
- // 判断是否为结束针
- bool IsEof = (recBytes[start]>> 7)> 0;
- // 暂不处理超过一帧的数据
- if (!IsEof) return "";
- start++;
- // 是否包含掩码
- bool hasMask = (recBytes[start]>> 7)> 0;
- // 不包含掩码的暂不处理
- if (!hasMask) return "";
- // 获取数据长度
- UInt64 mPackageLength = (UInt64) recBytes[start] & 0x7F;
- start++;
- // 存储 4 位掩码值
- byte[] Masking_key = new byte[4];
- // 存储数据
- byte[] mDataPackage;
- if (mPackageLength == 126) {
- // 等于 126 随后的两个字节 16 位表示数据长度
- mPackageLength = (UInt64)(recBytes[start] <<8 | recBytes[start + 1]);
- start += 2;
- }
- if (mPackageLength == 127) {
- // 等于 127 随后的八个字节 64 位表示数据长度
- mPackageLength = (UInt64)(recBytes[start] << (8 * 7) | recBytes[start] << (8 * 6) | recBytes[start] << (8 * 5) | recBytes[start] << (8 * 4) | recBytes[start] << (8 * 3) | recBytes[start] << (8 * 2) | recBytes[start] << 8 | recBytes[start + 1]);
- start += 8;
- }
- mDataPackage = new byte[mPackageLength];
- for (UInt64 i = 0; i < mPackageLength; i++) {
- mDataPackage[i] = recBytes[i + (UInt64) start + 4];
- }
- Buffer.BlockCopy(recBytes, start, Masking_key, 0, 4);
- for (UInt64 i = 0; i < mPackageLength; i++) {
- mDataPackage[i] = (byte)(mDataPackage[i] ^ Masking_key[i % 4]);
- }
- return Encoding.UTF8.GetString(mDataPackage);
- }
- #endregion
- #region 发送数据
- /// <summary>
- /// 把发送给客户端消息打包处理 (拼接上谁什么时候发的什么消息)
- /// </summary>
- /// <returns>The data.</returns>
- /// <param name="message">Message.</param>
- private byte[] SendMsg(string msg) {
- byte[] content = null;
- byte[] temp = Encoding.UTF8.GetBytes(msg);
- if (temp.Length <126) {
- content = new byte[temp.Length + 2];
- content[0] = 0x81;
- content[1] = (byte) temp.Length;
- Buffer.BlockCopy(temp, 0, content, 2, temp.Length);
- } else if (temp.Length < 0xFFFF) {
- content = new byte[temp.Length + 4];
- content[0] = 0x81;
- content[1] = 126;
- content[2] = (byte)(temp.Length & 0xFF);
- content[3] = (byte)(temp.Length>> 8 & 0xFF);
- Buffer.BlockCopy(temp, 0, content, 4, temp.Length);
- }
- return content;
- }
- #endregion
来源: https://www.cnblogs.com/yanbigfeg/p/9323270.html