public void OnChannelClosed(object sender, StreamStoppedEventArgs args)
        {
            var channel = (Channel)sender;

            switch (args.StopReason)
            {
            case StopReason.OffAir: {
                var msg = new NotificationMessage(
                    channel.ChannelInfo.Name,
                    "チャンネルが終了しました",
                    NotificationMessageType.Info);
                NotifyMessage(msg);
            }
            break;

            case StopReason.NoHost:
            case StopReason.ConnectionError: {
                var msg = new NotificationMessage(
                    channel.ChannelInfo.Name,
                    "チャンネルに接続できませんでした",
                    NotificationMessageType.Error);
                NotifyMessage(msg);
            }
            break;
            }
        }
        async void OnStreamStopped(object sender, StreamStoppedEventArgs args)
        {
            var disconnectMessage = args.DisconnectMessage?.ToString() ?? "no disconnection reason";
            var errorMessage      = args.Exception?.Message ?? "no error (clean stop).";

            var status = $"Twitter stream stopped {disconnectMessage}... {errorMessage}";

            _logger.LogInformation(status);

            await SendStatusUpdateAsync(status);
        }
        public async Task FilteredStream()
        {
            if (!EndToEndTestConfig.ShouldRunEndToEndTests)
            {
                return;
            }

            var stream = _tweetinviTestClient.Streams.CreateFilteredStream();

            stream.AddTrack("twitter");

            ITweet tweet = null;
            StreamStoppedEventArgs streamStoppedEventArgs = null;

            stream.MatchingTweetReceived += (sender, args) =>
            {
                tweet = args.Tweet;
                _logger.WriteLine($"Tweet matched via {args.MatchOn.ToString()}");
                _logger.WriteLine(tweet.ToString());
                stream.Stop();
            };

            stream.EventReceived += (sender, args) => { _logger.WriteLine(args.Json); };
            stream.StreamStopped += (sender, args) => { streamStoppedEventArgs = args; };

            var runStreamTask = Task.Run(async() =>
            {
                _logger.WriteLine("Before starting stream");
                await stream.StartMatchingAllConditionsAsync();
                _logger.WriteLine("Stream completed");
            });

            var delayTask = Task.Delay(TimeSpan.FromSeconds(15));

            var task = await Task.WhenAny(runStreamTask, delayTask);

            if (task != runStreamTask)
            {
                throw new TimeoutException();
            }

            _logger.WriteLine(streamStoppedEventArgs.Exception?.ToString() ?? "No exception");
            _logger.WriteLine(streamStoppedEventArgs.DisconnectMessage?.ToString() ?? "No disconnect message");

            Assert.Null(streamStoppedEventArgs.Exception);
            Assert.Null(streamStoppedEventArgs.DisconnectMessage);
            Assert.NotNull(tweet);

            await Task.Delay(TimeSpan.FromSeconds(10)); // this is for preventing Enhance Your Calm message from Twitter
        }
        public async Task TweetStream()
        {
            if (!EndToEndTestConfig.ShouldRunEndToEndTests)
            {
                return;
            }

            var stream = _tweetinviTestClient.Streams.CreateTweetStream();

            ITweet tweet = null;
            StreamStoppedEventArgs streamStoppedEventArgs = null;

            stream.TweetReceived += (sender, args) =>
            {
                tweet = args.Tweet;
                _logger.WriteLine(tweet.ToString());
                stream.Stop();
            };

            stream.EventReceived += (sender, args) => { _logger.WriteLine(args.Json); };
            stream.StreamStopped += (sender, args) => { streamStoppedEventArgs = args; };

            var runStreamTask = Task.Run(async() =>
            {
                _logger.WriteLine("Before starting stream");
                await stream.StartAsync("https://stream.twitter.com/1.1/statuses/sample.json");
                _logger.WriteLine("Stream completed");
            });

            var delayTask = Task.Delay(TimeSpan.FromSeconds(15));

            var task = await Task.WhenAny(runStreamTask, delayTask);

            if (task != runStreamTask)
            {
                throw new TimeoutException();
            }

            _logger.WriteLine(streamStoppedEventArgs.Exception?.ToString() ?? "No exception");
            _logger.WriteLine(streamStoppedEventArgs.DisconnectMessage?.ToString() ?? "No disconnect message");

            Assert.Null(streamStoppedEventArgs.Exception);
            Assert.Null(streamStoppedEventArgs.DisconnectMessage);
            Assert.NotNull(tweet);

            await Task.Delay(4000); // this is for preventing Enhance Your Calm message from Twitter
        }
Beispiel #5
0
        private void StreamTaskStateChanged(object sender, StreamTaskStateChangedEventArgs args)
        {
            var streamState = args.State;

            switch (streamState)
            {
            case StreamState.Running:
                this.Raise(StreamResumed);
                break;

            case StreamState.Pause:
                this.Raise(StreamPaused);
                break;

            case StreamState.Stop:
                var streamStoppedEventArgs = new StreamStoppedEventArgs(args.Exception);

                this.Raise(StreamStopped, streamStoppedEventArgs);
                break;
            }
        }
Beispiel #6
0
        public void StopStream(Exception exception, IDisconnectMessage disconnectMessage)
        {
            lock (_lockStream)
            {
                if (StreamState != StreamState.Stop)
                {
                    StopStreamAndUnsubscribeFromEvents();

                    if (exception is ITwitterTimeoutException && disconnectMessage == null)
                    {
                        disconnectMessage = new DisconnectMessage
                        {
                            Code   = 503,
                            Reason = "Timeout"
                        };
                    }

                    var streamExceptionEventArgs = new StreamStoppedEventArgs(exception, disconnectMessage);
                    this.Raise(StreamStopped, streamExceptionEventArgs);
                }
            }
        }