public static void Initialize(EventHandler <TweetEventArgs> onEventReceived = null, EventHandler <TweetEventArgs> onSourceDown = null, EventHandler <TweetEventArgs> onSourceUp = null) { if (initialized) { Debug.LogWarning("Trying to initialize when source is already initialized."); return; } source = TweetEventSource.CreateFilterStream(); if (onEventReceived != null) { source.EventReceived += onEventReceived; } if (onEventReceived != null) { source.SourceDown += onSourceDown; } if (onEventReceived != null) { source.SourceUp += onSourceUp; } var config = source.AuthConfig; config.ConsumerKey = CONSUMER_PUBLIC; config.ConsumerSecret = CONSUMER_PRIVATE; config.Token = API_PUBLIC; config.TokenSecret = API_PRIVATE; Debug.Log("Stream initialized"); initialized = true; }
static void Main(string[] args) { try { Console.WriteLine("===== Application Started ====="); // Step 1: Create TweetEventSource and wire some event handlers. var source = TweetEventSource.CreateFilterStream(); source.EventReceived += new EventHandler <TweetEventArgs>(source_EventReceived); source.SourceUp += new EventHandler <TweetEventArgs>(source_SourceUp); source.SourceDown += new EventHandler <TweetEventArgs>(source_SourceDown); // Step 2: Load the configuration into event source LoadTwitterKeysFromConfig(source); // Step 3: Main loop, e.g. retries 5 times at most int retryCount = 0; while (retryCount++ < 5) { // Step 4: Starts the event source. This starts another thread that pulls data from Twitter to our queue. source.Start(new StreamingAPIParameters() { Track = new string[] { "Thailand" } }); // Step 5: While our event source is Active, dispatches events while (source.Active) { source.Dispatch(1000); // This fires EventReceived callback on this thread } // Step 6: Source is inactive. Ensure stop and cleanup things source.Stop(); // Step 7: Wait for some time before attempt reconnect Console.WriteLine("=== Disconnected, wait for {0} ms before reconnect ===", Program.waitReconectTime); Thread.Sleep(Program.waitReconectTime); } Console.WriteLine("===== Application Ended ====="); } catch (ConfigurationErrorsException cex) { Console.Error.WriteLine(@"Error reading config: If you're running this for the first time, " + "please make sure you have your version of Twitter.config at application's " + "working directory - " + cex.Message); Trace.TraceError("Read config failed: " + cex.ToString()); } catch (Exception ex) { Console.Error.WriteLine("Unknown error: " + ex.Message); Trace.TraceError("Unknown error: " + ex.ToString()); } }
private static void LoadTwitterKeysFromConfig(TweetEventSource source) { var settings = System.Configuration.ConfigurationManager.AppSettings; var config = source.AuthConfig; config.ConsumerKey = settings["ConsumerKey"]; config.ConsumerSecret = settings["ConsumerSecret"]; config.Token = settings["Token"]; config.TokenSecret = settings["TokenSecret"]; // These are default values: // config.OAuthVersion = "1.0"; // config.SignatureMethod = "HMAC-SHA1"; Console.WriteLine(config.ToString()); }
private void InitialiseAndConfigureTweetEventSource(TweetEventSource tweetEventSource) { _tweetEventSource = tweetEventSource; _tweetEventSource.EventReceived += OnTweetReceived; _tweetEventSource.SourceDown += OnSourceDown; }
public HomeController(TweetEventSource tweetEventSource, IPusher pusher) { _pusher = pusher; InitialiseAndConfigureTweetEventSource(tweetEventSource); }