public void Start(IObserver <StreamingMessage> observer, StreamingApi client, StreamingType type, KeyValuePair <string, object>[] parameters)
        {
            var token = this.cancel.Token;

            client.IncludedTokens.SendStreamingRequestAsync(GetMethodType(type), client.GetUrl(type), parameters, token)
            .Done(res => res.GetResponseStreamAsync(), token)
            .Unwrap()
            .Done(stream =>
            {
                using (var reader = new StreamReader(stream))
                    using (token.Register(reader.Dispose))
                    {
                        foreach (var s in reader.EnumerateLines().Where(x => !string.IsNullOrEmpty(x)))
                        {
                            try
                            {
                                observer.OnNext(StreamingMessage.Parse(s));
                            }
                            catch (ParsingException ex)
                            {
                                observer.OnNext(RawJsonMessage.Create(s, ex));
                            }
                        }
                    }
                observer.OnCompleted();
            }, token, true)
            .ContinueWith(t =>
            {
                if (!token.IsCancellationRequested)
                {
                    if (t.Exception != null)
                    {
                        observer.OnError(t.Exception.InnerExceptions.Count == 1 ? t.Exception.InnerException : t.Exception);
                    }
                    else if (t.IsCanceled)
                    {
                        observer.OnError(new TaskCanceledException(t));
                    }
                }
            });
        }
Example #2
0
        public async void Start(IObserver <StreamingMessage> observer, StreamingApi client, StreamingType type, KeyValuePair <string, object>[] parameters)
        {
            var token = this.cancel.Token;

            try
            {
                // Make sure that all the operations are run in background
                var firstTask = Task.Run(() => client.IncludedTokens.SendStreamingRequestAsync(GetMethodType(type), client.GetUrl(type), parameters, token), token);

                using (var res = await firstTask.ConfigureAwait(false))
                    using (var reader = new StreamReader(await res.GetResponseStreamAsync().ConfigureAwait(false)))
                    {
                        while (!reader.EndOfStream)
                        {
                            if (token.IsCancellationRequested)
                            {
                                return;
                            }

                            var line = await reader.ReadLineAsync().ConfigureAwait(false);

                            if (!string.IsNullOrEmpty(line))
                            {
                                StreamingMessage message;
                                try
                                {
                                    message = StreamingMessage.Parse(line);
                                }
                                catch (ParsingException ex)
                                {
                                    message = RawJsonMessage.Create(line, ex);
                                }

                                if (token.IsCancellationRequested)
                                {
                                    return;
                                }
                                observer.OnNext(message);
                            }
                        }
                    }

                observer.OnCompleted();
            }
            catch (Exception ex)
            {
                if (!token.IsCancellationRequested)
                {
                    observer.OnError(ex);
                }
            }
        }