diff --git a/Tweetinvi.Core/Core/Streaming/IStreamResultGenerator.cs b/Tweetinvi.Core/Core/Streaming/IStreamResultGenerator.cs index c41d859f2..e4f2901ce 100644 --- a/Tweetinvi.Core/Core/Streaming/IStreamResultGenerator.cs +++ b/Tweetinvi.Core/Core/Streaming/IStreamResultGenerator.cs @@ -32,6 +32,12 @@ public interface IStreamResultGenerator /// event EventHandler StreamStopped; + /// + /// A keep-alive message has been received. + /// Twitter sends these every 30s so we know the stream's still working. + /// + event EventHandler KeepAliveReceived; + /// /// Get the current state of the stream analysis /// diff --git a/Tweetinvi.Core/Core/Streaming/ITwitterStream.cs b/Tweetinvi.Core/Core/Streaming/ITwitterStream.cs index 667e33624..6837aeaf3 100644 --- a/Tweetinvi.Core/Core/Streaming/ITwitterStream.cs +++ b/Tweetinvi.Core/Core/Streaming/ITwitterStream.cs @@ -29,6 +29,12 @@ public interface ITwitterStream /// event EventHandler StreamStopped; + /// + /// A keep-alive message has been received. + /// Twitter sends these every 30s so we know the stream's still working. + /// + event EventHandler KeepAliveReceived; + /// /// A tweet has been deleted. /// diff --git a/Tweetinvi.Streams/Helpers/StreamResultGenerator.cs b/Tweetinvi.Streams/Helpers/StreamResultGenerator.cs index 450832463..1cb8bf90a 100644 --- a/Tweetinvi.Streams/Helpers/StreamResultGenerator.cs +++ b/Tweetinvi.Streams/Helpers/StreamResultGenerator.cs @@ -21,6 +21,7 @@ public class StreamResultGenerator : IStreamResultGenerator public event EventHandler StreamResumed; public event EventHandler StreamPaused; public event EventHandler StreamStopped; + public event EventHandler KeepAliveReceived; private IStreamTask _currentStreamTask; private readonly IFactory _streamTaskFactory; @@ -84,6 +85,7 @@ public async Task StartStreamAsync(Func processObject, Func @@ -188,6 +190,7 @@ private StreamExceptionEventArgs StopStreamAndUnsubscribeFromEvents() { streamTask.StreamStarted -= StreamTaskStarted; streamTask.StreamStateChanged -= StreamTaskStateChanged; + streamTask.KeepAliveReceived -= KeepAliveReceived; streamTask.Stop(); if (_currentStreamTask == streamTask) diff --git a/Tweetinvi.Streams/StreamTask.cs b/Tweetinvi.Streams/StreamTask.cs index b02b7e0ea..7c0a1ba78 100644 --- a/Tweetinvi.Streams/StreamTask.cs +++ b/Tweetinvi.Streams/StreamTask.cs @@ -21,6 +21,7 @@ public interface IStreamTask { event EventHandler StreamStarted; event EventHandler> StreamStateChanged; + event EventHandler KeepAliveReceived; StreamState StreamState { get; } Exception LastException { get; } @@ -35,6 +36,7 @@ public class StreamTask : IStreamTask { public event EventHandler StreamStarted; public event EventHandler> StreamStateChanged; + public event EventHandler KeepAliveReceived; // https://dev.twitter.com/streaming/overview/connecting#stalls private const int STREAM_DISCONNECTED_DELAY = 90000; @@ -122,6 +124,7 @@ public void Start() { if (json == string.Empty) { + this.Raise(KeepAliveReceived); continue; } diff --git a/Tweetinvi.Streams/TwitterStream.cs b/Tweetinvi.Streams/TwitterStream.cs index 30bfda510..631da8a7f 100644 --- a/Tweetinvi.Streams/TwitterStream.cs +++ b/Tweetinvi.Streams/TwitterStream.cs @@ -106,6 +106,11 @@ public event EventHandler StreamStopped add { _streamResultGenerator.StreamStopped += value; } remove { _streamResultGenerator.StreamStopped -= value; } } + public event EventHandler KeepAliveReceived + { + add { _streamResultGenerator.KeepAliveReceived += value; } + remove { _streamResultGenerator.KeepAliveReceived -= value; } + } public event EventHandler TweetDeleted; public event EventHandler TweetLocationInfoRemoved;