<PackageReference Include="System.ClientModel" Version="1.1.0-beta.7" />

ServerSentEventEnumerable

Represents a collection of SSE events that can be enumerated as a C# collection.
using System.Collections; using System.Collections.Generic; using System.IO; using System.Runtime.CompilerServices; using System.Threading; namespace System.ClientModel.Internal { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] internal class ServerSentEventEnumerable : IEnumerable<ServerSentEvent>, IEnumerable { [System.Runtime.CompilerServices.Nullable(0)] private sealed class ServerSentEventEnumerator : IEnumerator<ServerSentEvent>, IEnumerator, IDisposable { private readonly ServerSentEventReader _reader; private readonly ServerSentEventEnumerable _enumerable; public ServerSentEvent Current { get; set; } object IEnumerator.Current { get { return Current; } } public ServerSentEventEnumerator(Stream contentStream, ServerSentEventEnumerable enumerable) { _reader = new ServerSentEventReader(contentStream); _enumerable = enumerable; } public bool MoveNext() { ServerSentEvent? nullable = _reader.TryGetNextEvent(default(CancellationToken)); _enumerable.LastEventId = _reader.LastEventId; _enumerable.ReconnectionInterval = _reader.ReconnectionInterval; if (nullable.HasValue) { Current = nullable.Value; return true; } Current = default(ServerSentEvent); return false; } public void Reset() { throw new NotSupportedException("Cannot seek back in an SSE stream."); } public void Dispose() { } } private readonly Stream _contentStream; public string LastEventId { get; set; } public TimeSpan ReconnectionInterval { get; set; } public ServerSentEventEnumerable(Stream contentStream) { Argument.AssertNotNull(contentStream, "contentStream"); _contentStream = contentStream; LastEventId = string.Empty; ReconnectionInterval = Timeout.InfiniteTimeSpan; } public IEnumerator<ServerSentEvent> GetEnumerator() { return new ServerSentEventEnumerator(_contentStream, this); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } }