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. Unlike
SseConnExt::with_sse_stream, this can send heartbeat comments, because it obtains a
runtime from the server at startup.
The conn is passed through untouched — continuing on to subsequent handlers — if the request’s
Accept header excludes text/event-stream, or if SseHandler::connect returns None. A
request with no Accept header accepts anything, per RFC 9110 §12.5.1.
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.
This is a contract about what the server sends, not a guarantee about the connection. Regular traffic makes an idle stream less likely to be dropped by an intermediary, and makes the server notice a departed client sooner — it only learns of one when it next writes — but neither is assured.
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 before_send(&self, conn: Conn) -> impl Future<Output = Conn> + Send
fn before_send(&self, conn: Conn) -> impl Future<Output = Conn> + Send
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§fn upgrade(&self, upgrade: Upgrade) -> impl Future<Output = ()> + Send
fn upgrade(&self, upgrade: Upgrade) -> impl Future<Output = ()> + Send
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.