Example #1
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}");
            }
        }
        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
            {
                NettyBody testEvent = MessagePackSerializer.Deserialize <NettyBody>(MessagePackSerializer.Serialize(msg));

                // 收到发送给服务端的心跳包,服务端回应
                if (testEvent.code == (int)NettyCodeEnum.Ping)
                {
                    ClientEventHandler.LstSendPings.Clear();
                    ClientEventHandler.RecordLogEvent?.Invoke(true, "收到Android端心跳回应");
                    return;
                }
                // 发送数据给服务端,服务端处理成功回应
                if (testEvent.code == (int)NettyCodeEnum.OK)
                {
                    lock (ClientEventHandler.LockOjb)
                    {
                        ClientEventHandler.LstNeedSendDatas.RemoveAll(cu => cu.NettyBody.reqId == testEvent.reqId);
                    }
                    return;
                }
                // 收到服务端发送过来的聊天内容
                if (testEvent.code == (int)NettyCodeEnum.Chat)
                {
                    ClientEventHandler.ReceiveEventFromClientEvent?.Invoke(testEvent);
                }
                var eventMsg = JsonConvert.SerializeObject(testEvent);
                ClientEventHandler.RecordLogEvent?.Invoke(true, $"收到Android端消息:{eventMsg}");
            }
            catch (Exception ex)
            {
                ClientEventHandler.RecordLogEvent?.Invoke(false, $"读取数据异常:{ex.Message}");
            }
        }
 private void ReceiveMessage(NettyBody testEvent)
 {
     App.Current.Dispatcher.Invoke(() =>
     {
         ChatInfoModel info = new ChatInfoModel
         {
             Message  = testEvent.data,
             SenderId = "ddd",
             Type     = ChatMessageType.String,
             Role     = ChatRoleType.Receiver
         };
         ChatInfos.Add(info);
     });
 }
Example #4
0
 /// <summary>
 /// 发送数据到服务端
 /// </summary>
 /// <param name="nettyBody"></param>
 public static void SendData(NettyBody nettyBody)
 {
     try
     {
         lock (LockOjb)
         {
             LstNeedSendDatas.Add(new NettyBodyCounter {
                 NettyBody = nettyBody
             });
         }
     }
     catch (Exception ex)
     {
         RecordLogEvent?.Invoke(false, $"发送数据异常:{ex.Message}");
     }
 }
Example #5
0
        /// <summary>
        /// 发送心跳包
        /// </summary>
        /// <param name="ctx"></param>
        public static void SendPingMsg(IChannelHandlerContext ctx)
        {
            // 发送的ping,超过一定范围,认为与服务端断开连接,需要重连
            if (LstSendPings.Count >= RETRY_SEND_PINT_TIME)
            {
                ctx.CloseAsync();
                RecordLogEvent?.Invoke(false, $"{LstSendPings.Count} 次未收到心跳回应,重连服务器");
                LstSendPings.Clear();
                return;
            }
            string guid = System.Guid.NewGuid().ToString();

            LstSendPings.Enqueue(guid);
            ctx.WriteAndFlushAsync(NettyBody.ping(guid));
            RecordLogEvent?.Invoke(true, $"发送心跳包,已发送{LstSendPings.Count} 次");
        }
Example #6
0
 /// <summary>
 /// 发送数据到客户端
 /// </summary>
 /// <param name="testEvent"></param>
 public void SendData(NettyBody testEvent)
 {
     try
     {
         if (channelHandlerContext == null)
         {
             RecordLogEvent?.Invoke(false, $"未连接客户端,无法发送数据");
             return;
         }
         channelHandlerContext.WriteAndFlushAsync(testEvent);
     }
     catch (Exception ex)
     {
         RecordLogEvent?.Invoke(false, $"发送数据异常:{ex.Message}");
     }
 }