private void HandleWebSocketFrame(IChannelHandlerContext context, WebSocketFrame frame)
        {
            // Check for closing frame
            if (frame is CloseWebSocketFrame)
            {
                handshaker.CloseAsync(context.Channel, (CloseWebSocketFrame)frame.Retain());
                return;
            }

            if (frame is PingWebSocketFrame)
            {
                context.WriteAsync(new PongWebSocketFrame((IByteBuffer)frame.Content.Retain()));
                return;
            }

            if (frame is TextWebSocketFrame)
            {
                // Echo the frame
                context.WriteAsync(frame.Retain());
                return;
            }

            if (frame is BinaryWebSocketFrame)
            {
                // Echo the frame
                context.WriteAsync(frame.Retain());
            }
        }
Example #2
0
        void HandleWebSocketFrame(IChannelHandlerContext ctx, WebSocketFrame frame)
        {
            var appSession = _appSessionContainer.Get(ctx.Channel.Id.ToString());

            if (appSession == null)
            {
                throw new ArgumentNullException(nameof(appSession));
            }

            appSession.LastAccessTime = DateTime.Now;
            switch (frame)
            {
            // Check for closing frame
            case CloseWebSocketFrame _:
                _handshaker.CloseAsync(ctx.Channel, (CloseWebSocketFrame)frame.Retain());
                return;

            case PingWebSocketFrame _:
                ctx.WriteAsync(new PongWebSocketFrame((IByteBuffer)frame.Content.Retain()));
                return;

            case TextWebSocketFrame _:
                ctx.WriteAsync(frame.Retain());
                return;

            case BinaryWebSocketFrame _:
                HandleBinaryWebSocketFrame((BinaryWebSocketFrame)frame.Retain(), appSession);
                return;
            }
        }
Example #3
0
        private void HandleWebSocketFrame(IChannelHandlerContext ctx, WebSocketFrame frame)
        {
            // Check for closing frame
            if (frame is CloseWebSocketFrame)
            {
                handshaker.CloseAsync(ctx.Channel, (CloseWebSocketFrame)frame.Retain());
                return;
            }

            if (frame is PingWebSocketFrame)
            {
                ctx.WriteAsync(new PongWebSocketFrame((IByteBuffer)frame.Content.Retain()));
                return;
            }

            if (frame is TextWebSocketFrame textFrame)
            {
                _server.connectionDict.TryGetValue(ctx.Channel.Id.AsShortText(), out WebSocketConnection conn);
                _server._event.OnConnectionReceiveTextAction?.Invoke(conn, textFrame.Text());
                return;
            }

            if (frame is BinaryWebSocketFrame)
            {
                _server.connectionDict.TryGetValue(ctx.Channel.Id.AsShortText(), out WebSocketConnection conn);
                _server._event.OnConnectionReceiveBinaryAction?.Invoke(conn, frame.Content.ToBytes());
            }
        }
Example #4
0
        void HandleWebSocketFrame(IChannelHandlerContext ctx, IByteBufferHolder frame)
        {
            switch (frame)
            {
            case CloseWebSocketFrame _:
                _handShaker.CloseAsync(ctx.Channel, (CloseWebSocketFrame)frame.Retain());
                ConsoleHelper.ConDepServerWriteLine($"连接{ctx.Channel.Id}已断开");
                return;

            case PingWebSocketFrame _:
                ctx.WriteAsync(new PongWebSocketFrame((IByteBuffer)frame.Content.Retain()));
                return;

            case PongWebSocketFrame _:
                ctx.WriteAsync(new PingWebSocketFrame((IByteBuffer)frame.Content.Retain()));
                return;

            case TextWebSocketFrame _:
                try
                {
                    string commandString = frame.Content.ReadString(frame.Content.WriterIndex, Encoding.UTF8);
                    var    command       = commandString.JsonToObject <Command>();
                    if (_webSocketConfig.NotShowCommandBlackList == null || _webSocketConfig.NotShowCommandBlackList.Length == 0 || !_webSocketConfig.NotShowCommandBlackList.Contains(command.HandlerName))
                    {
                        ConsoleHelper.ConDepServerWriteLine(command.HandlerName);
                    }

                    if (CanLoginSuccess(command))
                    {
                        Task.Run(async() => await SendCommand(ctx, commandString, command));
                    }
                    else
                    {
                        Task.Run(async() =>
                        {
                            var @event = new ServerErrorEvent
                            {
                                Status  = 401,
                                Message = "未登录"
                            };
                            await ctx.Channel.SendJsonEventAsync(@event);
                        });
                    }
                }
                catch (Exception ex)
                {
                    ConsoleHelper.ConDepServerErrorWriteLine(ex);
                    _logger.LogCritical(ex, ex.Message);
                }
                return;

            case BinaryWebSocketFrame binaryFrame:
                ctx.WriteAndFlushAsync(binaryFrame.Retain());
                break;
            }
        }
        private async void HandleWebSocketFrame(IChannelHandlerContext ctx, WebSocketFrame frame)
        {
            try
            {
                // Check for closing frame
                if (frame is CloseWebSocketFrame)
                {
                    await _handshaker.CloseAsync(ctx.Channel, (CloseWebSocketFrame)frame.Retain());

                    return;
                }

                if (frame is PingWebSocketFrame)
                {
                    await ctx.WriteAsync(new PongWebSocketFrame((IByteBuffer)frame.Content.Retain()));

                    return;
                }

                if (frame is TextWebSocketFrame)
                {
                    // Echo the frame
                    var revBuffer  = frame.Content as IByteBuffer;
                    var dataBuffer = new byte[revBuffer.ReadableBytes];
                    revBuffer.ReadBytes(dataBuffer);
                    var msg = MOMsg.Parser.ParseFrom(ByteString.FromBase64(Encoding.UTF8.GetString(dataBuffer)));
                    if (msg != null)
                    {
                        await _session.DispatchIncomingPacket(msg);
                    }
                    return;
                }

                if (frame is BinaryWebSocketFrame)
                {
                    // Echo the frame
                    var revBuffer  = frame.Content as IByteBuffer;
                    var dataBuffer = new byte[revBuffer.ReadableBytes];
                    revBuffer.ReadBytes(dataBuffer);
                    var msg = MOMsg.Parser.ParseFrom(dataBuffer);
                    if (msg != null)
                    {
                        await _session.DispatchIncomingPacket(msg);
                    }
                    return;
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(
                    $"ChannelRead Exception:\n" +
                    $"{ex.Message}\n" +
                    $"{ex.StackTrace}");
            }
        }
Example #6
0
        void HandleWebSocketFrame(IChannelHandlerContext ctx, IByteBufferHolder frame)
        {
            switch (frame)
            {
            case CloseWebSocketFrame _:
                Task.Run(async() => {
                    await _handShaker.CloseAsync(ctx.Channel, (CloseWebSocketFrame)frame.Retain());
                });
                return;

            case PingWebSocketFrame _:
                ctx.WriteAsync(new PongWebSocketFrame((IByteBuffer)frame.Content.Retain()));
                return;

            case PongWebSocketFrame _:
                return;

            case TextWebSocketFrame _:
                try
                {
                    string commandString = frame.Content.ReadString(frame.Content.WriterIndex, Encoding.UTF8);
                    var    command       = commandString.JsonToObject <Command>();
                    var    commandBus    = TestServerHelper.ServiceProvider.GetRequiredService <ICommandBus>();
                    Task.Run(async() => {
                        await commandBus.SendAsync(ctx, commandString, command);
                    });
                }
                catch (Exception ex)
                {
                    ConsoleHelper.TestWriteLine(ex.Message, "未能解析事件");
                }
                return;

            case BinaryWebSocketFrame _:
                ctx.WriteAsync(frame.Retain());
                break;
            }
        }
        protected override void ChannelRead0(IChannelHandlerContext ctx, WebSocketFrame frame)
        {
            WebSocketServerHandshaker handshaker = ctx.GetAttribute(wshsKey).Get();

            // Check for closing frame
            if (frame is CloseWebSocketFrame)
            {
                handshaker.CloseAsync(ctx.Channel, (CloseWebSocketFrame)frame.Retain());
                return;
            }

            if (frame is PingWebSocketFrame)
            {
                ctx.WriteAsync(new PongWebSocketFrame((IByteBuffer)frame.Content.Retain()));
                return;
            }

            if (frame is TextWebSocketFrame text)
            {
                var uri     = text.Text();
                var textmsg = messageFactory.Parse(uri);
                if (textmsg != null)
                {
                    ctx.FireChannelRead(textmsg);
                }
                return;
            }

            if (frame is BinaryWebSocketFrame)
            {
                var binarymsg = messageFactory.Parse(frame.Content);
                if (binarymsg != null)
                {
                    ctx.FireChannelRead(binarymsg);
                }
                return;
            }
        }