12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net;
- using System.IO;
- using System.Threading;
- using System.Diagnostics;
- namespace Client.SSE
- {
- public class ConnectingState : IConnectionState
- {
- private Uri mUrl;
- private IWebRequesterFactory mWebRequesterFactory;
- private Dictionary<string, string> headers;
- public EventSourceState State { get { return EventSourceState.CONNECTING; } }
- public ConnectingState(Uri url, IWebRequesterFactory webRequesterFactory, Dictionary<string, string> headers)
- {
- if (url == null) throw new ArgumentNullException("Url cant be null");
- if (webRequesterFactory == null) throw new ArgumentNullException("Factory cant be null");
- mUrl = url;
- mWebRequesterFactory = webRequesterFactory;
- this.headers = headers;
- }
- public Task<IConnectionState> Run(Action<ServerSentEvent> donothing, CancellationToken cancelToken, Dictionary<string, string> headers)
- {
- IWebRequester requester = mWebRequesterFactory.Create();
- var taskResp = requester.Get(mUrl, headers);
- return taskResp.ContinueWith<IConnectionState>(tsk =>
- {
- if (tsk.Status == TaskStatus.RanToCompletion && !cancelToken.IsCancellationRequested)
- {
- IServerResponse response = tsk.Result;
- if (response.StatusCode == HttpStatusCode.OK)
- {
- return new ConnectedState(response, mWebRequesterFactory, headers);
- }
- else
- {
- Trace.WriteLine("Failed to connect to: " + mUrl.ToString() + response ?? (" Http statuscode: " + response.StatusCode));
- }
- }
- return new DisconnectedState(mUrl, mWebRequesterFactory, headers);
- });
- }
- }
- }
|