/// <summary>
        /// 收到请求数据
        /// </summary>
        /// <param name="frame">请求数据</param>
        private void OnFrameRequest(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:
                this.OnBinary(frame);
                break;

            case FrameCodes.Text:
                this.OnText(frame);
                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;
            }
        }
예제 #2
0
        /// <summary>
        /// 收到到数据帧请求
        /// </summary>
        /// <param name="context">会话对象</param>
        /// <param name="frameRequest">数据帧</param>
        /// <returns></returns>
        private async Task OnWebSocketRequestAsync(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:
                await this.OnBinaryAsync(context, frameRequest.Content);

                break;

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

                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;
            }
        }
예제 #3
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;
            }
        }