示例#1
0
        /// <summary>
        /// Method to start a stream from a Token
        /// </summary>
        /// <param name="token">Token to get the credentials to query the stream</param>
        public void StartStream(Token token)
        {
            if (_isRunning)
            {
                throw new OperationCanceledException("You cannot run the stream multiple times");
            }

            #region Variables
            long     todayTicks = DateTime.Now.Ticks;
            DateTime init       = new DateTime(todayTicks - todayTicks % TimeSpan.TicksPerDay + 1);

            state = StreamState.Resume;
            HttpWebRequest webRequest = token.GenerateRequest(new Uri(StreamUrl));
            StreamReader   reader     = init_webRequest(webRequest);

            string jsonTweet;
            int    error_occured = 0;
            #endregion

            while (state != StreamState.Stop)
            {
                try
                {
                    jsonTweet = null;
                    jsonTweet = reader.ReadLine();

                    #region Error Checking
                    if (jsonTweet == null || jsonTweet == "")
                    {
                        if (error_occured == 0)
                        {
                            ++error_occured;
                        }
                        else if (error_occured == 1)
                        {
                            ++error_occured;
                            webRequest.Abort();
                            reader = init_webRequest(webRequest);
                        }
                        else if (error_occured == 2)
                        {
                            ++error_occured;
                            webRequest.Abort();
                            webRequest = token.GenerateRequest(new Uri(StreamUrl));
                            reader     = init_webRequest(webRequest);
                        }
                        else
                        {
                            Console.WriteLine("Twitter API is not accessible");
                            Trace.WriteLine("Twitter API is not accessible");
                            break;
                        }
                    }
                    else if (error_occured != 0)
                    {
                        error_occured = 0;
                    }
                    #endregion
                    Tweet tweet = Tweet.Create(jsonTweet);

                    if (processTweetDelegate != null)
                    {
                        processTweetDelegate(tweet, false);
                    }
                }
                catch (IOException ex)
                {
                    // Verify the implementation of the Exception handler
                    #region IOException Handler
                    if (ex.Message == "Unable to read data from the transport connection: The connection was closed.")
                    {
                        reader = init_webRequest(webRequest);
                    }

                    try
                    {
                        jsonTweet = reader.ReadLine();
                    }
                    catch (IOException ex2)
                    {
                        if (ex2.Message == "Unable to read data from the transport connection: The connection was closed.")
                        {
                            Trace.WriteLine("Streamreader was unable to read from the stream!");
                            if (processTweetDelegate != null)
                            {
                                processTweetDelegate(null, true);
                            }
                            break;
                        }
                    }
                    #endregion
                }
            }

            #region Clean
            webRequest.Abort();
            reader.Dispose();
            state = StreamState.Stop;
            #endregion
        }