Handler

Trait Handler 

Source
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§

Source

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 message
  • conn - The connection that sent the message
  • state - The application state
  • extensions - Request-scoped extension data
§Returns
  • Ok(Some(message)) - Send this message back to the client
  • Ok(None) - Don’t send any response
  • Err(error) - An error occurred during processing

Implementors§

Source§

impl<F, Fut, Res> Handler for HandlerService<F, ()>
where F: Fn() -> Fut + Send + Sync + 'static, Fut: Future<Output = Res> + Send + 'static, Res: IntoResponse,

Source§

impl<F, Fut, Res, T1> Handler for HandlerService<F, (T1,)>
where F: Fn(T1) -> Fut + Send + Sync + 'static, Fut: Future<Output = Res> + Send + 'static, Res: IntoResponse, T1: FromMessage + Send + 'static,

Source§

impl<F, Fut, Res, T1, T2> Handler for HandlerService<F, (T1, T2)>
where F: Fn(T1, T2) -> Fut + Send + Sync + 'static, Fut: Future<Output = Res> + Send + 'static, Res: IntoResponse, T1: FromMessage + Send + 'static, T2: FromMessage + Send + 'static,

Source§

impl<F, Fut, Res, T1, T2, T3> Handler for HandlerService<F, (T1, T2, T3)>
where F: Fn(T1, T2, T3) -> Fut + Send + Sync + 'static, Fut: Future<Output = Res> + Send + 'static, Res: IntoResponse, T1: FromMessage + Send + 'static, T2: FromMessage + Send + 'static, T3: FromMessage + Send + 'static,

Source§

impl<F, Fut, Res, T1, T2, T3, T4> Handler for HandlerService<F, (T1, T2, T3, T4)>
where F: Fn(T1, T2, T3, T4) -> Fut + Send + Sync + 'static, Fut: Future<Output = Res> + Send + 'static, Res: IntoResponse, T1: FromMessage + Send + 'static, T2: FromMessage + Send + 'static, T3: FromMessage + Send + 'static, T4: FromMessage + Send + 'static,

Source§

impl<F, Fut, Res, T1, T2, T3, T4, T5> Handler for HandlerService<F, (T1, T2, T3, T4, T5)>
where F: Fn(T1, T2, T3, T4, T5) -> Fut + Send + Sync + 'static, Fut: Future<Output = Res> + Send + 'static, Res: IntoResponse, T1: FromMessage + Send + 'static, T2: FromMessage + Send + 'static, T3: FromMessage + Send + 'static, T4: FromMessage + Send + 'static, T5: FromMessage + Send + 'static,

Source§

impl<F, Fut, Res, T1, T2, T3, T4, T5, T6> Handler for HandlerService<F, (T1, T2, T3, T4, T5, T6)>
where F: Fn(T1, T2, T3, T4, T5, T6) -> Fut + Send + Sync + 'static, Fut: Future<Output = Res> + Send + 'static, Res: IntoResponse, T1: FromMessage + Send + 'static, T2: FromMessage + Send + 'static, T3: FromMessage + Send + 'static, T4: FromMessage + Send + 'static, T5: FromMessage + Send + 'static, T6: FromMessage + Send + 'static,

Source§

impl<F, Fut, Res, T1, T2, T3, T4, T5, T6, T7> Handler for HandlerService<F, (T1, T2, T3, T4, T5, T6, T7)>
where F: Fn(T1, T2, T3, T4, T5, T6, T7) -> Fut + Send + Sync + 'static, Fut: Future<Output = Res> + Send + 'static, Res: IntoResponse, T1: FromMessage + Send + 'static, T2: FromMessage + Send + 'static, T3: FromMessage + Send + 'static, T4: FromMessage + Send + 'static, T5: FromMessage + Send + 'static, T6: FromMessage + Send + 'static, T7: FromMessage + Send + 'static,

Source§

impl<F, Fut, Res, T1, T2, T3, T4, T5, T6, T7, T8> Handler for HandlerService<F, (T1, T2, T3, T4, T5, T6, T7, T8)>
where F: Fn(T1, T2, T3, T4, T5, T6, T7, T8) -> Fut + Send + Sync + 'static, Fut: Future<Output = Res> + Send + 'static, Res: IntoResponse, T1: FromMessage + Send + 'static, T2: FromMessage + Send + 'static, T3: FromMessage + Send + 'static, T4: FromMessage + Send + 'static, T5: FromMessage + Send + 'static, T6: FromMessage + Send + 'static, T7: FromMessage + Send + 'static, T8: FromMessage + Send + 'static,