pub struct SseParser { /* private fields */ }Expand description
Incremental Server-Sent Events parser.
§Example
use stream_rs::sse::SseParser;
let mut p = SseParser::new();
let mut events = Vec::new();
// Note the chunk boundary splitting the CRLF and a multi-line data field.
p.feed(b"event: greeting\r\ndata: hello\r", &mut events);
p.feed(b"\ndata: world\r\n\r\n", &mut events);
assert_eq!(events.len(), 1);
assert_eq!(events[0].event.as_deref(), Some("greeting"));
assert_eq!(events[0].data, "hello\nworld");Implementations§
Source§impl SseParser
impl SseParser
Sourcepub fn last_id(&self) -> Option<&str>
pub fn last_id(&self) -> Option<&str>
The persistent last event id.
Per the WHATWG spec this survives across dispatches: once an id field
is seen it is reported on every subsequent event until replaced. This
getter exposes the current value so a caller can send it as the
Last-Event-ID header when reconnecting. Note that finish
resets the parser, clearing this — read it before calling finish if
you need it for reconnection.
Sourcepub fn reconnection_time(&self) -> Option<u64>
pub fn reconnection_time(&self) -> Option<u64>
The current reconnection time in milliseconds, from the most recent valid
retry field.
Unlike the per-event SseEvent::retry, this is connection-level state
that persists across dispatched events (and across events that carried no
data and were therefore never surfaced), matching the WHATWG semantics.
Sourcepub fn feed(&mut self, chunk: &[u8], out: &mut Vec<SseEvent>)
pub fn feed(&mut self, chunk: &[u8], out: &mut Vec<SseEvent>)
Feed a chunk of bytes. Completed events are pushed onto out.
Bytes that do not yet form a complete line are buffered internally until the next call.
Sourcepub fn finish(&mut self, out: &mut Vec<SseEvent>)
pub fn finish(&mut self, out: &mut Vec<SseEvent>)
Signal end of stream.
Per the WHATWG algorithm, once the stream ends the final line (bytes received after the last terminator) is processed as a field, but a pending event is only dispatched by a blank line — so an event that was never terminated by a blank line is discarded. This call therefore applies any trailing unterminated field, then drops the in-progress event buffers without emitting them.
out exists for API symmetry with feed; by spec nothing
is ever pushed onto it here, but the signature lets callers funnel both
calls through the same sink.
§Reset semantics
finish fully resets the parser so it can be reused for a fresh stream.
This clears the persistent state too — the last event id
(last_id) and the reconnection time
(reconnection_time). If you need either for a
reconnection, read it before calling finish.