public override bool IsSharable => true;//标注一个channel handler可以被多个channel安全地共享。

        /// <summary>
        /// 收到服务端回应消息
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="msg"></param>
        protected override void ChannelRead0(IChannelHandlerContext ctx, object msg)
        {
            try
            {
                TestEvent testEvent = MessagePackSerializer.Deserialize <TestEvent>(MessagePackSerializer.Serialize(msg));
                if (testEvent.code == EventCode.Ping)
                {
                    lstSendPings.Clear();
                    RecordLogEvent?.Invoke("收到Android端心跳回应");
                    return;
                }
                else if (testEvent.code == EventCode.OK)
                {
                    lock (lockOjb)
                    {
                        lstNeedSendDatas.RemoveAll(cu => cu.TestEvent.reqId == testEvent.reqId);
                    }
                }
                else if (testEvent.code == EventCode.Chat)
                {
                    ReceiveEventFromClientEvent?.Invoke(testEvent);
                }
                var eventMsg = JsonConvert.SerializeObject(testEvent);
                RecordLogEvent?.Invoke($"收到Android端消息:{eventMsg}");
            }
            catch (Exception ex)
            {
                RecordLogEvent?.Invoke($"读取数据异常:{ex.Message}");
            }
        }
Beispiel #2
0
        public override bool IsSharable => true;//标注一个channel handler可以被多个channel安全地共享。

        /// <summary>
        /// 收到客户端回应
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="msg"></param>
        protected override void ChannelRead0(IChannelHandlerContext ctx, object msg)
        {
            channelHandlerContext = ctx;
            try
            {
                NettyBody testEvent = MessagePackSerializer.Deserialize <NettyBody>(MessagePackSerializer.Serialize(msg));

                // 服务端收到心跳,直接回应
                if (testEvent.code == (int)NettyCodeEnum.Ping)
                {
                    ctx.WriteAndFlushAsync(msg);
                    RecordLogEvent?.Invoke(true, $"收到心跳并原文回应:{ctx.Channel.RemoteAddress}");
                    return;
                }
                if (testEvent.code == (int)NettyCodeEnum.Chat)
                {
                    RecordLogEvent?.Invoke(true, $"服务端接收到聊天消息({ctx.Channel.RemoteAddress}):" + JsonConvert.SerializeObject(testEvent));

                    // 回应收到消息成功
                    testEvent.code = (int)NettyCodeEnum.OK;
                    ctx.WriteAndFlushAsync(testEvent);
                    ReceiveEventFromClientEvent?.Invoke(testEvent);
                    return;
                }
                RecordLogEvent?.Invoke(true, $"服务端接收到消息({ctx.Channel.RemoteAddress}):" + JsonConvert.SerializeObject(testEvent));
            }
            catch (Exception ex)
            {
                RecordLogEvent?.Invoke(false, $"收到客户端消息,处理失败({ctx.Channel.RemoteAddress}):{ex.Message}");
            }
        }
Beispiel #3
0
        public override bool IsSharable => true;//标注一个channel handler可以被多个channel安全地共享。

        /// <summary>
        /// 收到客户端回应
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="msg"></param>
        protected override void ChannelRead0(IChannelHandlerContext ctx, object msg)
        {
            try
            {
                TestEvent testEvent = MessagePackSerializer.Deserialize <TestEvent>(MessagePackSerializer.Serialize(msg));

                // 服务端收到心跳,直接回应
                if (testEvent.code == EventCode.Ping)
                {
                    ctx.WriteAndFlushAsync(msg);
                    RecordLogEvent?.Invoke($"收到心跳并原文回应");
                    return;
                }
                else if (testEvent.code == EventCode.Chat)
                {
                    // 回应收到消息成功
                    testEvent.code = EventCode.OK;
                    ctx.WriteAndFlushAsync(testEvent);
                    ReceiveEventFromClientEvent?.Invoke(testEvent);
                }
                RecordLogEvent?.Invoke($"服务端接收到消息:" + JsonConvert.SerializeObject(testEvent));
            }
            catch (Exception ex)
            {
                RecordLogEvent?.Invoke($"收到客户端消息,处理失败:{ex.Message}");
            }
        }
        public override bool IsSharable => true;//标注一个channel handler可以被多个channel安全地共享。

        /// <summary>
        /// 收到客户端回应
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="msg"></param>
        protected override void ChannelRead0(IChannelHandlerContext ctx, ChatInfo msg)
        {
            try
            {
                // 服务端收到心跳,直接回应
                if (msg.Code == (int)NettyCodeEnum.Ping)
                {
                    ctx.WriteAndFlushAsync(msg);
                    RecordLogEvent?.Invoke(true, $"收到心跳并原文回应:{ctx.Channel.RemoteAddress}");
                    return;
                }
                if (msg.Code == (int)NettyCodeEnum.Chat)
                {
                    RecordLogEvent?.Invoke(true, $"服务端接收到聊天消息({ctx.Channel.RemoteAddress}):" + JsonConvert.SerializeObject(msg));

                    foreach (var kvp in dictClients)
                    {
                        if (ctx.Channel.RemoteAddress.ToString() == kvp.Key)
                        {
                            // 回应收到消息成功
                            msg.Code = (int)NettyCodeEnum.OK;
                        }
                        else
                        {
                            // 群发消息
                            msg.Code = (int)NettyCodeEnum.Chat;
                        }
                        kvp.Value.WriteAndFlushAsync(msg);
                    }
                    msg.FromId = ctx.Channel.RemoteAddress.ToString();
                    ReceiveEventFromClientEvent?.Invoke(msg);
                    return;
                }
                RecordLogEvent?.Invoke(true, $"服务端接收到消息({ctx.Channel.RemoteAddress}):" + JsonConvert.SerializeObject(msg));

            }
            catch (Exception ex)
            {
                RecordLogEvent?.Invoke(false, $"收到客户端消息,处理失败({ctx.Channel.RemoteAddress}):{ex.Message}");
            }
        }