C#编程之C# WebSocket解析(收发数据包、分片超长包处理)
小标 2018-11-14 来源 : 阅读 5539 评论 0

摘要:本文主要向大家介绍了C#编程之C# WebSocket解析(收发数据包、分片超长包处理),通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。

本文主要向大家介绍了C#编程之C#  WebSocket解析(收发数据包、分片超长包处理),通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace LaiHuaRendSpeederServer
{
    public static class WebSocketSeverHelper
    {
        /// 


        /// 打包服务器握手数据
        /// 


        public static byte[] PackageHandShakeData(string handShakeText)
        {
            //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" + Environment.NewLine);
            responseBuilder.Append("Upgrade: websocket" + Environment.NewLine);
            responseBuilder.Append("Connection: Upgrade" + Environment.NewLine);
            responseBuilder.Append("Sec-WebSocket-Accept: " + secKey + Environment.NewLine + Environment.NewLine);
            //responseBuilder.Append("Sec-WebSocket-Protocol: " + "chat, superchat" + Environment.NewLine + Environment.NewLine);
           

            return Encoding.UTF8.GetBytes(responseBuilder.ToString());
        }

        /// 


        /// 解析客户端发送来的数据
        /// 


        public static string DecodeClientData(byte[] recBytes, int length)
        {
            if (length < 2)
            {
                return string.Empty;
            }
            
            bool fin = (recBytes[0] & 0x80) == 0x80; //0x80 = 1000,0000  第1bit = 1表示最后一帧  
            if (!fin)
            {
                if (recBytes[1] == 0xff)
                {
                }
                else
                return string.Empty;
            }

            bool mask_flag = (recBytes[1] & 0x80) == 0x80; // 是否包含掩码  
            if (!mask_flag)
            {
                return string.Empty;// 不包含掩码的暂不处理
            }

            int payload_len = recBytes[1] & 0x7F; // 数据长度  

            byte[] masks = new byte[4];
            byte[] payload_data;

            if (payload_len == 126)
            {
                Array.Copy(recBytes, 4, masks, 0, 4);
                payload_len = (UInt16)(recBytes[2] << 8 | recBytes[3]);
                payload_data = new byte[payload_len];
                Array.Copy(recBytes, 8, payload_data, 0, payload_len);

            }
            else if (payload_len == 127)
            {
                Array.Copy(recBytes, 10, masks, 0, 4);
                byte[] uInt64Bytes = new byte[8];
                for (int i = 0; i < 8; i++)
                {
                    uInt64Bytes[i] = recBytes[9 - i];
                }
                UInt64 len = BitConverter.ToUInt64(uInt64Bytes, 0);

                payload_data = new byte[len];
                for (UInt64 i = 0; i < len; i++)
                {
                    payload_data[i] = recBytes[i + 14];
                }
            }
            else
            {
                Array.Copy(recBytes, 2, masks, 0, 4);
                payload_data = new byte[payload_len];
                Array.Copy(recBytes, 6, payload_data, 0, payload_len);

            }

            for (var i = 0; i < payload_len; i++)
            {
                payload_data[i] = (byte)(payload_data[i] ^ masks[i % 4]);
            }
            //var uuu = new byte[payload_data.Length * 3 / 4];
            //for (int i = 0; i < uuu.Length; i++)
            //{
            //    uuu[i] = payload_data[i];
            //}
            //Console.WriteLine("UUUUUU:" + Encoding.UTF8.GetString(uuu));
            return Encoding.UTF8.GetString(payload_data);
        }

        public static byte[] DecodeClientByteData(byte[] recBytes, int length)
        {
            if (length < 2)
            {
                return null;
            }

            bool fin = (recBytes[0] & 0x80) == 0x80; //0x80 = 1000,0000  第1bit = 1表示最后一帧  
            //if (!fin)
            //{
            //    if (recBytes[1] == 0xff)
            //    {
            //        if (recBytes[0] == 0x01)
            //        {
            //            recBytes[0] += 0x80;
            //        }
            //        else
            //            return null;
            //    }
            //    else
            //        return null;
            //}

            bool mask_flag = (recBytes[1] & 0x80) == 0x80; // 是否包含掩码  
            if (!mask_flag)
            {
                return null;// 不包含掩码的暂不处理
            }

            int payload_len = recBytes[1] & 0x7F; // 数据长度  

            byte[] masks = new byte[4];
            byte[] payload_data;

            if (payload_len == 126)
            {
                Array.Copy(recBytes, 4, masks, 0, 4);
                payload_len = (UInt16)(recBytes[2] << 8 | recBytes[3]);
                payload_data = new byte[payload_len];
                Array.Copy(recBytes, 8, payload_data, 0, payload_len);

            }
            else if (payload_len == 127)
            {
                Array.Copy(recBytes, 10, masks, 0, 4);
                byte[] uInt64Bytes = new byte[8];
                for (int i = 0; i < 8; i++)
                {
                    uInt64Bytes[i] = recBytes[9 - i];
                }
                UInt64 len = BitConverter.ToUInt64(uInt64Bytes, 0);

                payload_data = new byte[len];
                
                for (UInt64 i = 0; i < len; i++)
                {
                    payload_data[i] = recBytes[i + 14];
                }
            }
            else
            {
                Array.Copy(recBytes, 2, masks, 0, 4);
                payload_data = new byte[payload_len];
                Array.Copy(recBytes, 6, payload_data, 0, payload_len);

            }

            for (var i = 0; i < payload_data.Length; i++)
            {
                payload_data[i] = (byte)(payload_data[i] ^ masks[i % 4]);
            }
            return payload_data;
        }

        /// 


        /// 把客户端消息打包处理
        /// 


        public static byte[] EncodeServerData(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;
                Array.Copy(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);
                Array.Copy(temp, 0, content, 4, temp.Length);
            }
            else
            {
                // 暂不处理超长内容  
            }
            return content;
        }
    }
}


using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;

namespace LaiHuaRendSpeederServer
{
    public static class WebSocketExt
    {
        public static int SendProtocol(this Socket s, Protocol p)
        {
            var msg = JsonConvert.SerializeObject(p);
            Console.WriteLine("SendProtocol : " + msg);
            return s.Send(WebSocketSeverHelper.EncodeServerData(msg));
        }
        public static int ReceiveProtocol(this Socket s, ref Protocol p)
        {
            byte[] buffer = new byte[502400];
            int reccount = 0;
            List<ArraySegment

> segList = new List<ArraySegment>();
            if (s.Connected)
            {
                reccount = s.Receive(buffer);
                if(reccount == 0)
                {
                    s.Shutdown(SocketShutdown.Send);
                    p = null;
                    return 0;
                }
            }
            byte[] r = new byte[reccount];
            Array.Copy(buffer, r, reccount);
            buffer = null;
            string mgs = string.Empty;
            if ((r[0] & 0x80) == 0)
            {
                segList.Add(new ArraySegment(r, 0, reccount));
                byte[] r2 = new byte[8];
                while ((r2[0] & 0x80) == 0)
                {
                    r2 = new byte[502400];
                    int cout = s.Receive(r2);
                    segList.Add(new ArraySegment(r2, 0, cout));
                }
                byte[] all = null;
                for (int i = 0; i < segList.Count; i++)
                {
                    if (all == null)
                    {
                        all = WebSocketSeverHelper.DecodeClientByteData(segList[i].Array, segList[i].Count);
                    }
                    else
                    {
                        var pit = WebSocketSeverHelper.DecodeClientByteData(segList[i].Array, segList[i].Count);
                        all = all.Concat(pit).ToArray();
                    }
                }
                mgs = Encoding.UTF8.GetString(all);
            }
            else
            {
                mgs = WebSocketSeverHelper.DecodeClientData(r, r.Length);
            }

            if (mgs.Length < 150)
            {
                Console.WriteLine("ReceiveProtocol : " + mgs);
            }
            else
            {
                Console.WriteLine("ReceiveProtocol : " + mgs.Substring(0, 150));
            }
            try
            {
                var f = mgs.Substring(1, mgs.Length - 2);
                var menbs = f.Split(new string[] { "\",\"" }, StringSplitOptions.RemoveEmptyEntries);
                if (menbs.Length != 4)
                {
                    p = null;
                    return reccount;
                }
                p = new Protocol();
                foreach (var item in menbs)
                {
                    var pare = item.Split(new string[] { "\":\"" }, StringSplitOptions.None);
                    if (pare.Length != 2)
                    {
                        p = null;
                        return reccount;
                    }
                    switch (pare[0].Replace("\"", ""))
                    {
                        case "Head":
                            p.Head = pare[1].Replace("\"", "");
                            break;
                        case "Func":
                            p.Func = pare[1].Replace("\"", "");
                            break;
                        case "Name":
                            p.Name = pare[1].Replace("\"", "");
                            break;
                        case "Data":
                            p.Data = pare[1].Replace("\"", "");
                            break;
                        default:
                            p = null;
                            return reccount;
                            break;
                    }
                }
                //p = JsonConvert.DeserializeObject(mgs);
            }
            catch (Exception ex)
            {
                var errormsg = JsonConvert.SerializeObject(
                    new Protocol() { Func = FUNC.ERROR, Data = ex.ToString() });
                if (s.Connected)
                {
                    s.Send(WebSocketSeverHelper.EncodeServerData(errormsg));
                }
                Console.WriteLine("ERROR : " + errormsg);
                s.Close();
            }
            return reccount;
        }
    }
}

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 3
看完这篇文章有何感觉?已经有4人表态,25%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程