From 79b16b95738358dbd5db2e8b52e0af3f8d0fd5d8 Mon Sep 17 00:00:00 2001 From: Josh Keegan Date: Wed, 10 May 2017 11:01:49 +0100 Subject: [PATCH] Implement #506 - event for a keep-alive being received on a stream --- Tweetinvi.Core/Core/Streaming/IStreamResultGenerator.cs | 6 ++++++ Tweetinvi.Core/Core/Streaming/ITwitterStream.cs | 6 ++++++ Tweetinvi.Streams/Helpers/StreamResultGenerator.cs | 3 +++ Tweetinvi.Streams/StreamTask.cs | 3 +++ Tweetinvi.Streams/TwitterStream.cs | 5 +++++ 5 files changed, 23 insertions(+) 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 165e0cba0..eabb5d775 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 e3d644812..43e82a2ce 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 @@ -190,6 +192,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 8b3d40ea9..19d0b11d0 100644 --- a/Tweetinvi.Streams/TwitterStream.cs +++ b/Tweetinvi.Streams/TwitterStream.cs @@ -91,6 +91,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;