Esempio n. 1
0
 public AbstractWsClient(NTMinerAppType appType)
 {
     _appType = appType;
     VirtualRoot.AddEventPath <Per1SecondEvent>("重试Ws连接的秒表倒计时", LogEnum.None, action: message => {
         if (NextTrySecondsDelay > 0)
         {
             NextTrySecondsDelay--;
         }
     }, typeof(VirtualRoot));
     VirtualRoot.AddEventPath <Per10SecondEvent>("周期检查Ws连接", LogEnum.None,
                                                 action: message => {
         NTMinerRegistry.SetDaemonActiveOn(DateTime.Now);
         if (_continueCount >= _failConnCount)
         {
             _continueCount = 0;
             OpenOrCloseWs();
         }
         else
         {
             _continueCount++;
         }
     }, typeof(VirtualRoot));
     VirtualRoot.AddEventPath <Per20SecondEvent>("周期Ws Ping", LogEnum.None, action: message => {
         try {
             if (_ws != null && _ws.ReadyState == WebSocketState.Open)
             {
                 // 或者_ws.IsAlive,因为_ws.IsAlive内部也是一个Ping,所以用Ping从而显式化这里有个网络请求
                 if (!_ws.Ping())
                 {
                     _ws.CloseAsync(CloseStatusCode.Away);
                 }
             }
         }
         catch (Exception e) {
             Logger.ErrorDebugLine(e);
         }
     }, typeof(VirtualRoot));
     VirtualRoot.AddEventPath <AppExitEvent>("退出程序时关闭Ws连接", LogEnum.DevConsole, action: message => {
         _ws?.CloseAsync(CloseStatusCode.Normal, "客户端程序退出");
     }, this.GetType());
     OpenOrCloseWs();
 }