Skip to content

Commit

Permalink
Merge branch 'JoshKeegan-streamKeepAlive' into 1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
linvi committed May 21, 2017
2 parents 5174916 + 79b16b9 commit 46e1172
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Tweetinvi.Core/Core/Streaming/IStreamResultGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public interface IStreamResultGenerator
/// </summary>
event EventHandler<StreamExceptionEventArgs> StreamStopped;

/// <summary>
/// A keep-alive message has been received.
/// Twitter sends these every 30s so we know the stream's still working.
/// </summary>
event EventHandler KeepAliveReceived;

/// <summary>
/// Get the current state of the stream analysis
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions Tweetinvi.Core/Core/Streaming/ITwitterStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public interface ITwitterStream
/// </summary>
event EventHandler<StreamExceptionEventArgs> StreamStopped;

/// <summary>
/// A keep-alive message has been received.
/// Twitter sends these every 30s so we know the stream's still working.
/// </summary>
event EventHandler KeepAliveReceived;

/// <summary>
/// A tweet has been deleted.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions Tweetinvi.Streams/Helpers/StreamResultGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class StreamResultGenerator : IStreamResultGenerator
public event EventHandler StreamResumed;
public event EventHandler StreamPaused;
public event EventHandler<StreamExceptionEventArgs> StreamStopped;
public event EventHandler KeepAliveReceived;

private IStreamTask _currentStreamTask;
private readonly IFactory<IStreamTask> _streamTaskFactory;
Expand Down Expand Up @@ -84,6 +85,7 @@ public async Task StartStreamAsync(Func<string, bool> processObject, Func<ITwitt
_currentStreamTask = streamTask;
_currentStreamTask.StreamStarted += StreamTaskStarted;
_currentStreamTask.StreamStateChanged += StreamTaskStateChanged;
_currentStreamTask.KeepAliveReceived += KeepAliveReceived;
}
#if NET_CORE
await Task.Run(() =>
Expand Down Expand Up @@ -188,6 +190,7 @@ private StreamExceptionEventArgs StopStreamAndUnsubscribeFromEvents()
{
streamTask.StreamStarted -= StreamTaskStarted;
streamTask.StreamStateChanged -= StreamTaskStateChanged;
streamTask.KeepAliveReceived -= KeepAliveReceived;
streamTask.Stop();

if (_currentStreamTask == streamTask)
Expand Down
3 changes: 3 additions & 0 deletions Tweetinvi.Streams/StreamTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface IStreamTask
{
event EventHandler StreamStarted;
event EventHandler<GenericEventArgs<StreamState>> StreamStateChanged;
event EventHandler KeepAliveReceived;

StreamState StreamState { get; }
Exception LastException { get; }
Expand All @@ -35,6 +36,7 @@ public class StreamTask : IStreamTask
{
public event EventHandler StreamStarted;
public event EventHandler<GenericEventArgs<StreamState>> StreamStateChanged;
public event EventHandler KeepAliveReceived;

// https://dev.twitter.com/streaming/overview/connecting#stalls
private const int STREAM_DISCONNECTED_DELAY = 90000;
Expand Down Expand Up @@ -122,6 +124,7 @@ public void Start()
{
if (json == string.Empty)
{
this.Raise(KeepAliveReceived);
continue;
}

Expand Down
5 changes: 5 additions & 0 deletions Tweetinvi.Streams/TwitterStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ public event EventHandler<StreamExceptionEventArgs> StreamStopped
add { _streamResultGenerator.StreamStopped += value; }
remove { _streamResultGenerator.StreamStopped -= value; }
}
public event EventHandler KeepAliveReceived
{
add { _streamResultGenerator.KeepAliveReceived += value; }
remove { _streamResultGenerator.KeepAliveReceived -= value; }
}

public event EventHandler<TweetDeletedEventArgs> TweetDeleted;
public event EventHandler<TweetLocationDeletedEventArgs> TweetLocationInfoRemoved;
Expand Down

0 comments on commit 46e1172

Please sign in to comment.