Table of Contents

Class SseEventReader

Namespace
SseAssertions
Assembly
SseAssertions.dll

Reads Server-Sent Events from a Stream one at a time, as they arrive. Use it for a stream that never ends: read until the events you need have arrived, then stop, instead of waiting for the end of the stream or a timeout. The parsing rules are those of Parse(string).

public sealed class SseEventReader
Inheritance
SseEventReader
Inherited Members

Remarks

The body is decoded as UTF-8, as the SSE specification requires; a leading byte order mark is skipped.

On an SseStream, every reader shares the stream's position: a reader created after another one stopped continues with the next event, and no bytes are lost between them. On another stream, a reader buffers ahead; once you stop using it, read the rest of the stream through the same reader.

Not thread-safe.

Constructors

SseEventReader(Stream)

Initializes a reader over stream.

public SseEventReader(Stream stream)

Parameters

stream Stream

The stream to read. An SseStream is read from its current position.

Exceptions

ArgumentNullException

stream is null.

Properties

BytesRead

Gets the number of bytes this reader has read from the stream.

public long BytesRead { get; }

Property Value

long

TextExcerpt

Gets the start of the text this reader has decoded (about the first 256 characters, lines joined by \n), for failure messages.

public string TextExcerpt { get; }

Property Value

string

Methods

ReadAsync(CancellationToken)

Reads the next event, waiting until a complete one has arrived. A frame without a data: line is skipped, as the SSE specification requires.

public ValueTask<SseEvent?> ReadAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Cancels the wait for the next event.

Returns

ValueTask<SseEvent>

The next event, or null at the end of the stream.

Exceptions

OperationCanceledException

cancellationToken was canceled first.