Ejemplo n.º 1
0
        public SocketChangeTracker(ChangeTrackerOptions options) : base(options)
        {
            if (options.Mode == ChangeTrackerMode.LongPoll)
            {
                Continuous = true;
            }

            tokenSource                  = new CancellationTokenSource();
            _responseLogic               = ChangeTrackerResponseLogicFactory.CreateLogic(this);
            _responseLogic.OnCaughtUp    = () => Misc.IfNotNull(Client, c => c.ChangeTrackerCaughtUp(this));
            _responseLogic.OnChangeFound = (change) =>
            {
                if (!ReceivedChange(change))
                {
                    Log.To.ChangeTracker.W(Tag, "Received unparseable change from server {0}", new LogJsonString(change));
                }
            };

            _responseLogic.OnFinished = RetryOrStopIfNecessary;
        }
 // Called when the web socket connection is closed
 private void OnClose(object sender, CloseEventArgs args)
 {
     if (_client != null)
     {
         if (args.Code == (ushort)CloseStatusCode.ProtocolError)
         {
             // This is not a valid web socket connection, need to fall back to regular HTTP
             CanConnect = false;
             Stopped();
         }
         else
         {
             Log.To.ChangeTracker.I(Tag, "{0} remote {1} closed connection ({2} {3})",
                                    this, args.WasClean ? "cleanly" : "forcibly", args.Code, args.Reason);
             _responseLogic = new WebSocketLogic();
             Backoff.DelayAppropriateAmountOfTime().ContinueWith(t => _client.ConnectAsync());
         }
     }
     else
     {
         Log.To.ChangeTracker.I(Tag, "{0} is closed", this);
         Stopped();
     }
 }