Exemplo n.º 1
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;
            }
        }
Exemplo n.º 2
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;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 发送Http返回
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="byteBufferHolder"></param>
        /// <param name="response"></param>
        protected virtual async Task SendHttpResponseAsync(IChannelHandlerContext ctx, IByteBufferHolder byteBufferHolder, IFullHttpResponse response)
        {
            try
            {
                byteBufferHolder.Retain(0);
                if (ctx.Channel.Open)
                {
                    await ctx.Channel.WriteAndFlushAsync(response);

                    await ctx.CloseAsync();
                }
            }
            catch (Exception ex)
            {
                throw new MateralDotNettyException("发送HttpResponse失败", ex);
            }
        }