pub struct Sse<H> { /* private fields */ }Expand description
A Handler that responds to requests with a server-sent event stream.
Build one from any SseHandler with Sse::new or sse.
The conn is passed through untouched — continuing on to subsequent handlers — unless the
request’s Accept header names text/event-stream. Wildcard ranges and an absent
Accept header do not match, so the same route can also serve other representations.
When the client disconnects, the event stream is dropped: promptly on HTTP/1.x and HTTP/2, and at the next event or heartbeat on HTTP/3.
Implementations§
Source§impl<H: SseHandler> Sse<H>
impl<H: SseHandler> Sse<H>
Sourcepub fn new(handler: H) -> Self
pub fn new(handler: H) -> Self
Builds a new Sse handler from any SseHandler.
Sourcepub fn with_heartbeat(self, heartbeat: Duration) -> Self
pub fn with_heartbeat(self, heartbeat: Duration) -> Self
Sends an empty comment whenever heartbeat elapses without the stream yielding an event.
The interval is measured from the most recent event, not from the start of the response, so a busy stream sends no heartbeats at all. Clients discard the comment.
Regular traffic makes an idle stream less likely to be dropped by an intermediary. On HTTP/3 it also bounds how long a departed client goes unnoticed, since disconnection is detected there by a failed write.
Trait Implementations§
Source§impl<H: SseHandler> Handler for Sse<H>
impl<H: SseHandler> Handler for Sse<H>
Source§async fn run(&self, conn: Conn) -> Conn
async fn run(&self, conn: Conn) -> Conn
Source§async fn init(&mut self, info: &mut Info)
async fn init(&mut self, info: &mut Info)
Source§fn has_upgrade(&self, upgrade: &Upgrade) -> bool
fn has_upgrade(&self, upgrade: &Upgrade) -> bool
Handler::upgrade. The first handler that responds true to this will receive
ownership of the trillium::Upgrade in a subsequent call to
Handler::upgradeSource§async fn upgrade(&self, upgrade: Upgrade)
async fn upgrade(&self, upgrade: Upgrade)
Handler::has_upgrade and will
only be called once for this upgrade. There is no return value, and this function takes
exclusive ownership of the underlying transport once this is called. You can downcast
the transport to whatever the source transport type is and perform any non-http protocol
communication that has been negotiated. You probably don’t want this unless you’re
implementing something like websockets. Please note that for many transports such as
TcpStreams, dropping the transport (and therefore the Upgrade) will hang up /
disconnect.