DisconnectedState.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace Client.SSE
  8. {
  9. public class DisconnectedState : IConnectionState
  10. {
  11. private Uri mUrl;
  12. private IWebRequesterFactory mWebRequesterFactory;
  13. private Dictionary<string, string> headers;
  14. public EventSourceState State
  15. {
  16. get { return EventSourceState.CLOSED; }
  17. }
  18. public DisconnectedState(Uri url, IWebRequesterFactory webRequesterFactory, Dictionary<string, string> headers)
  19. {
  20. if (url == null) throw new ArgumentNullException("Url cant be null");
  21. mUrl = url;
  22. mWebRequesterFactory = webRequesterFactory;
  23. this.headers = headers;
  24. }
  25. public Task<IConnectionState> Run(Action<ServerSentEvent> donothing, CancellationToken cancelToken, Dictionary<string, string> headers)
  26. {
  27. if(cancelToken.IsCancellationRequested)
  28. return Task.Factory.StartNew<IConnectionState>(() => { return new DisconnectedState(mUrl, mWebRequesterFactory, headers); });
  29. else
  30. return Task.Factory.StartNew<IConnectionState>(() => { return new ConnectingState(mUrl, mWebRequesterFactory, headers); });
  31. }
  32. }
  33. }