Skip to main content

websocket_handler_with_state

Function websocket_handler_with_state 

Source
pub fn websocket_handler_with_state<S, F, Fut>(
    ws: WebSocketUpgrade,
    state: S,
    handler: F,
) -> Response
where S: Send + 'static, F: FnOnce(WebSocket, S) -> Fut + Send + 'static, Fut: Future<Output = ()> + Send + 'static,
Expand description

Helper to create a WebSocket handler with shared state.

async fn chat_socket(mut socket: WebSocket, state: AppState) {
    // access state.db, state.config, etc.
}

Router::new()
    .route("/ws/chat", get(|ws: WebSocketUpgrade, State(state): State<AppState>| {
        websocket_handler_with_state(ws, state, chat_socket)
    }))