/// <summary>
        /// 注销通道
        /// </summary>
        /// <param name="context"></param>
        public override void ChannelUnregistered(IChannelHandlerContext context)
        {
            base.ChannelUnregistered(context);
            ClientEventHandler.RecordLogEvent?.Invoke(false, $"注销通道:{context.Channel.RemoteAddress}");
            ClientEventHandler.IsConnect = false;
            Thread.Sleep(TimeSpan.FromSeconds(5));

            if (!ClientEventHandler.IsConnect)
            {
                NettyClient nettyClient = new NettyClient(serverIP, serverPort);
                nettyClient.ConnectServer().Wait();
            }
        }
예제 #2
0
 /// <summary>
 /// 发送数据
 /// </summary>
 public static void RunSendData(IChannelHandlerContext ctxTmp)
 {
     ctx = ctxTmp;
     if (isRunning)
     {
         return;
     }
     isRunning = true;
     ThreadPool.QueueUserWorkItem(sen =>
     {
         while (true)
         {
             Thread.Sleep(TimeSpan.FromSeconds(DATA_SEND_INTERVAL));
             if (!IsConnect)
             {
                 RecordLogEvent?.Invoke(false, $"未连接服务,无法正常发送数据包!");
                 NettyClient nettyClient = new NettyClient(_serverIP, _serverPort);
                 nettyClient.ConnectServer().Wait();
                 Thread.Sleep(TimeSpan.FromSeconds(20));
                 continue;
             }
             try
             {
                 NettyBodyCounter sendEvent = null;
                 lock (LockOjb)
                 {
                     for (int i = LstNeedSendDatas.Count - 1; i >= 0; i--)
                     {
                         var tmpNettyBody = LstNeedSendDatas[i];
                         if (tmpNettyBody.TryCount >= RETRY_SEND_DATA_TIME)
                         {
                             LstNeedSendDatas.Remove(tmpNettyBody);
                             RecordLogEvent?.Invoke(false, $"删除超时数据包(已发{tmpNettyBody.TryCount}次):{JsonConvert.SerializeObject(tmpNettyBody.NettyBody)}");
                         }
                     }
                     sendEvent = LstNeedSendDatas.FirstOrDefault();
                 }
                 if (sendEvent != null)
                 {
                     ctx.WriteAndFlushAsync(sendEvent.NettyBody);
                     RecordLogEvent?.Invoke(true, $"发送到服务端(已发{sendEvent.TryCount}次):{JsonConvert.SerializeObject(sendEvent.NettyBody)}");
                     sendEvent.TryCount++;
                 }
             }
             catch (Exception ex2)
             {
                 RecordLogEvent?.Invoke(false, $"发送到服务端异常:{ex2.Message}");
             }
         }
     });
 }