Example #1
0
        /// <summary>
        /// Create an observable object.
        /// </summary>
        static IObservable <StreamingMessage> ReactiveBase(this StreamingApi e, StreamingType type, StreamingParameters parameters = null)
        {
            return(Observable.Create <StreamingMessage>((observer, cancel) => Task.Factory.StartNew(() =>
            {
                var url = type == StreamingType.User ? "https://userstream.twitter.com/1.1/user.json" :
                          type == StreamingType.Site ? " https://sitestream.twitter.com/1.1/site.json " :
                          type == StreamingType.Filter ? "https://stream.twitter.com/1.1/statuses/filter.json" :
                          type == StreamingType.Sample ? "https://stream.twitter.com/1.1/statuses/sample.json" :
                          type == StreamingType.Firehose ? "https://stream.twitter.com/1.1/statuses/firehose.json" : "";

                cancel.ThrowIfCancellationRequested();

                using (var reader = Connect(e.IncludedTokens, parameters, type == StreamingType.Filter ? MethodType.Post : MethodType.Get, url))
                {
                    cancel.ThrowIfCancellationRequested();
                    cancel.Register(() => reader.Close());

                    foreach (var s in reader.EnumerateLines().Where(x => !string.IsNullOrEmpty(x)))
                    {
                        observer.OnNext(RawJsonMessage.Create(e.IncludedTokens, s));
                        observer.OnNext(StreamingMessage.Parse(e.IncludedTokens, s));
                    }
                }
            }, cancel, TaskCreationOptions.LongRunning, TaskScheduler.Default)));
        }
Example #2
0
        private static IObservable <StreamingMessage> AccessStreamingApiAsObservableImpl(StreamingApi e, StreamingType type, IEnumerable <KeyValuePair <string, object> > parameters)
        {
            return(Observable.Create <StreamingMessage>((observer, cancel) =>
            {
                return e.IncludedTokens.SendStreamingRequestAsync(e.GetMethodType(type), e.GetUrl(type), parameters, cancel)
                .ContinueWith(task =>
                {
                    if (task.IsFaulted)
                    {
                        task.Exception.InnerException.Rethrow();
                    }

                    return task.Result.GetResponseStreamAsync();
                }, cancel)
                .Unwrap()
                .ContinueWith(task =>
                {
                    if (task.IsFaulted)
                    {
                        task.Exception.InnerException.Rethrow();
                    }

                    try
                    {
                        using (var reader = new StreamReader(task.Result))
                            using (var reg = cancel.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));
                                    }
                                }
                            }
                    }
                    finally
                    {
                        cancel.ThrowIfCancellationRequested();
                    }
                }, cancel, TaskContinuationOptions.LongRunning, TaskScheduler.Default);
            }));
        }
Example #3
0
        /// <summary>
        /// Starts the stream.
        /// </summary>
        /// <returns>The observable object.</returns>
        /// <param name="e">Tokens.</param>
        /// <param name="type">Type of streaming API.</param>
        /// <param name="parameters">Parameters.</param>
        public static IObservable <StreamingMessage> StartObservableStream(this StreamingApi e, StreamingType type, StreamingParameters parameters = null)
        {
            if (parameters == null)
            {
                parameters = new StreamingParameters();
            }

            return(Observable.Create <StreamingMessage>((observer, cancel) =>
            {
                var url = type == StreamingType.User ? "https://userstream.twitter.com/1.1/user.json" :
                          type == StreamingType.Site ? " https://sitestream.twitter.com/1.1/site.json " :
                          type == StreamingType.Filter ? "https://stream.twitter.com/1.1/statuses/filter.json" :
                          type == StreamingType.Sample ? "https://stream.twitter.com/1.1/statuses/sample.json" :
                          type == StreamingType.Firehose ? "https://stream.twitter.com/1.1/statuses/firehose.json" : "";

                return e.IncludedTokens.SendStreamingRequestAsync(type == StreamingType.Filter ? MethodType.Post : MethodType.Get, url, parameters.Parameters, cancel)
                .ContinueWith(task =>
                {
                    if (task.IsFaulted)
                    {
                        task.Exception.InnerException.Rethrow();
                    }

                    return task.Result.GetResponseStreamAsync();
                }, cancel)
                .Unwrap()
                .ContinueWith(task =>
                {
                    if (task.IsFaulted)
                    {
                        task.Exception.InnerException.Rethrow();
                    }

                    try
                    {
                        using (var reader = new StreamReader(task.Result))
                            using (var reg = cancel.Register(() => reader.Dispose()))
                            {
                                foreach (var s in reader.EnumerateLines().Where(x => !string.IsNullOrEmpty(x)))
                                {
#if !DEBUG
                                    try
                                    {
#endif
                                    observer.OnNext(StreamingMessage.Parse(s));
#if !DEBUG
                                }
                                catch
                                {
                                    observer.OnNext(RawJsonMessage.Create(s));
                                }
#endif
                                }
                            }
                    }
                    finally
                    {
                        cancel.ThrowIfCancellationRequested();
                    }
                }, cancel, TaskContinuationOptions.LongRunning, TaskScheduler.Default);
            }));
        }