Skip to main content

Handler

Trait Handler 

Source
pub trait Handler: MaybeSend + 'static {
    // Required method
    fn message(&mut self, message: &Message<'_>);

    // Provided methods
    fn event(&mut self, event: Event) { ... }
    fn binary(&mut self, data: &[u8]) { ... }
    fn ready(&mut self) -> impl Future<Output = ()> + MaybeSend { ... }
}
Expand description

What the application does with what the server sends.

The client calls it from the connection’s own task, in the order the messages came, with each message read in place from its frame. It holds the connection while it runs: hand long work to a task of its own.

A closure taking &Message is a handler too.

Required Methods§

Source

fn message(&mut self, message: &Message<'_>)

A message for the application: everything the server sends but its own protocol messages, duplicates dropped and topic messages in order.

Provided Methods§

Source

fn event(&mut self, event: Event)

Something happened to the connection.

Source

fn binary(&mut self, data: &[u8])

Bytes the server application sent as they are (send_bytes).

Source

fn ready(&mut self) -> impl Future<Output = ()> + MaybeSend

Called before each frame is read once the server took the connection. Waiting here holds the server back while the application catches up; the heartbeat and sends go on meanwhile, and the wait may be dropped for them and begun again: it must lose nothing when dropped. Not called in the browser, which cannot hold a WebSocket back: there the frames wait in memory.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<F> Handler for F
where F: FnMut(&Message<'_>) + MaybeSend + 'static,