Ejemplo n.º 1
0
        /// <summary>
        /// starts a continues stream until cancellation token is sent in
        /// </summary>
        /// <param name="url"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task StartStreamAsync(string url, CancellationToken cancellationToken)
        {
            try
            {
                // reporting data
                var data = new ReportingData
                {
                    DateStartTime = DateTime.Now.TimeOfDay,
                };
                var request  = new HttpRequestMessage(HttpMethod.Get, url);
                var response = await _client.SendAsync(
                    request,
                    HttpCompletionOption.ResponseHeadersRead);

                var body = await response.Content.ReadAsStreamAsync();

                using var reader = new StreamReader(body);
                while (!reader.EndOfStream && !cancellationToken.IsCancellationRequested)
                {
                    var tweet        = reader.ReadLine();
                    var currentTweet = JsonConvert.DeserializeObject <TwitterResponse>(tweet);
                    // make sure the current tweet is not null
                    if (currentTweet != null && currentTweet.Data != null)
                    {
                        data.CurrentTweet = currentTweet;
                        OnProcessCompleted(data);// notify
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"Unable to process stream: {ex.Message}");
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// report on the data
 /// </summary>
 /// <param name="e"></param>
 private void OnProcessCompleted(ReportingData e)
 {
     ReportingData?.Invoke(this, e);
 }