/// <summary>
        /// 收到到数据帧请求
        /// </summary>
        /// <param name="context">会话对象</param>
        /// <param name="frameRequest">数据帧</param>
        private void OnWebSocketRequest(IContenxt context, FrameRequest frameRequest)
        {
            switch (frameRequest.Frame)
            {
            case FrameCodes.Close:
                var frame = new CloseRequest(frameRequest);
                this.OnClose(context, frame.StatusCode, frame.CloseReason);
                context.Session.Close();
                break;

            case FrameCodes.Binary:
                this.OnBinary(context, frameRequest);
                break;

            case FrameCodes.Text:
                this.OnText(context, frameRequest);
                break;

            case FrameCodes.Ping:
                try
                {
                    var pong        = new FrameResponse(FrameCodes.Pong, frameRequest.Content);
                    var pongContent = pong.ToArraySegment(mask: false);
                    context.Session.Send(pongContent);
                }
                catch (Exception)
                {
                }
                finally
                {
                    this.OnPing(context, frameRequest.Content);
                }
                break;

            case FrameCodes.Pong:
                context.Session.Publish("Pong", frameRequest.Content);
                this.OnPong(context, frameRequest.Content);
                break;

            default:
                break;
            }
        }
示例#2
0
        /// <summary>
        /// 正常关闭客户端
        /// </summary>
        /// <param name="code">关闭码</param>
        /// <param name="reason">原因</param>
        public void Close(StatusCodes code, string reason)
        {
            var codeBytes   = ByteConverter.ToBytes((ushort)(code), Endians.Big);
            var reasonBytes = Encoding.UTF8.GetBytes(reason ?? string.Empty);
            var content     = new byte[codeBytes.Length + reasonBytes.Length];

            Array.Copy(codeBytes, 0, content, 0, codeBytes.Length);
            Array.Copy(reasonBytes, 0, content, codeBytes.Length, reasonBytes.Length);
            var response = new FrameResponse(FrameCodes.Close, content);

            try
            {
                this.Send(response);
            }
            catch (Exception)
            {
            }
            finally
            {
                this.session.Close();
            }
        }
        /// <summary>
        /// 正常关闭客户端
        /// </summary>      
        /// <param name="code">关闭码</param>
        /// <param name="reason">原因</param>
        public void Close(StatusCodes code, string reason)
        {
            var codeBytes = ByteConverter.ToBytes((ushort)(code), Endians.Big);
            var reasonBytes = Encoding.UTF8.GetBytes(reason ?? string.Empty);
            var content = new byte[codeBytes.Length + reasonBytes.Length];

            Array.Copy(codeBytes, 0, content, 0, codeBytes.Length);
            Array.Copy(reasonBytes, 0, content, codeBytes.Length, reasonBytes.Length);
            var response = new FrameResponse(FrameCodes.Close, content);

            try
            {
                this.SendResponse(response);
            }
            catch (Exception)
            {
            }
            finally
            {
                this.Close();
            }
        }
示例#4
0
        /// <summary>
        /// 收到请求数据
        /// </summary>
        /// <param name="frame">请求数据</param>
        /// <returns></returns>
        private async Task OnFrameRequestAsync(FrameRequest frame)
        {
            switch (frame.Frame)
            {
            case FrameCodes.Close:
                var closeFrame = new CloseRequest(frame);
                this.OnClose(closeFrame.StatusCode, closeFrame.CloseReason);
                base.Close();
                break;

            case FrameCodes.Binary:
                await this.OnBinaryAsync(frame.Content);

                break;

            case FrameCodes.Text:
                var content = Encoding.UTF8.GetString(frame.Content);
                await this.OnTextAsync(content);

                break;

            case FrameCodes.Ping:
                var pongValue = new FrameResponse(FrameCodes.Pong, frame.Content);
                this.TrySend(pongValue);
                this.OnPing(frame.Content);
                break;

            case FrameCodes.Pong:
                this.ProcessPingPong(frame.Content);
                this.OnPong(frame.Content);
                break;

            default:
                break;
            }
        }
示例#5
0
        /// <summary>
        /// 发送二进制数据
        /// </summary>
        /// <param name="content">二进制数据</param>
        /// <exception cref="SocketException"></exception>
        public int SendBinary(byte[] content)
        {
            var bin = new FrameResponse(FrameCodes.Binary, content);

            return(this.Send(bin));
        }
示例#6
0
        /// <summary>
        /// 发送二进制数据
        /// </summary>
        /// <param name="content">二进制数据</param>
        /// <exception cref="SocketException"></exception>
        public void SendBinary(byte[] content)
        {
            var response = new FrameResponse(FrameCodes.Binary, content);

            this.Send(response);
        }
        /// <summary>
        /// ping指令
        /// 服务器将回复pong
        /// </summary>
        /// <param name="contents">内容</param>
        /// <returns></returns>
        public int Ping(byte[] contents)
        {
            var ping = new FrameResponse(FrameCodes.Ping, contents);

            return(this.Send(ping));
        }
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="frame"></param>
        /// <returns></returns>
        private int Send(FrameResponse frame)
        {
            var buffer = frame.ToArraySegment(mask: true);

            return(base.Send(buffer));
        }
 /// <summary>
 /// 发送文本消息
 /// </summary>     
 /// <param name="content">文本内容</param>
 /// <exception cref="SocketException"></exception>
 public void SendText(string content)
 {
     var bytes = content == null ? new byte[0] : Encoding.UTF8.GetBytes(content);
     var response = new FrameResponse(FrameCodes.Text, bytes);
     this.SendResponse(response);
 }
示例#10
0
 /// <summary>
 /// 发送二进制数据
 /// </summary>       
 /// <param name="content">二进制数据</param>
 /// <exception cref="SocketException"></exception>
 public void SendBinary(byte[] content)
 {
     var response = new FrameResponse(FrameCodes.Binary, content);
     this.SendResponse(response);
 }