pub trait Handler:
Send
+ Sync
+ 'static {
// Required method
fn call<'life0, 'async_trait>(
&'life0 self,
message: Message,
conn: Connection,
state: AppState,
extensions: Extensions,
) -> Pin<Box<dyn Future<Output = Result<Option<Message>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
}Expand description
Core trait for message handlers.
This trait is automatically implemented for async functions that match
the required signature. You typically don’t implement this trait directly;
instead, use the handler() function to wrap your async functions.
§Automatic Implementation
Any async function with up to 8 extractor parameters that returns
impl IntoResponse automatically implements this trait.
§Examples
use wsforge::prelude::*;
// This function automatically implements Handler
async fn my_handler(msg: Message, conn: Connection) -> Result<String> {
Ok(format!("Received from {}", conn.id()))
}
// Wrap it in a HandlerService
let handler = handler(my_handler);Required Methods§
Sourcefn call<'life0, 'async_trait>(
&'life0 self,
message: Message,
conn: Connection,
state: AppState,
extensions: Extensions,
) -> Pin<Box<dyn Future<Output = Result<Option<Message>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn call<'life0, 'async_trait>(
&'life0 self,
message: Message,
conn: Connection,
state: AppState,
extensions: Extensions,
) -> Pin<Box<dyn Future<Output = Result<Option<Message>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Processes a message and returns an optional response.
This method is called by the framework when a message is received. It extracts the required data and executes the handler logic.
§Arguments
message- The received WebSocket messageconn- The connection that sent the messagestate- The application stateextensions- Request-scoped extension data
§Returns
Ok(Some(message))- Send this message back to the clientOk(None)- Don’t send any responseErr(error)- An error occurred during processing