Skip to content

Commit

Permalink
Add ws example showing how to pass data to callback (#1185)
Browse files Browse the repository at this point in the history
  • Loading branch information
nylonicious committed Jul 23, 2022
1 parent 4558671 commit 1ace855
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions axum/src/extract/ws.rs
Expand Up @@ -36,6 +36,37 @@
//! # };
//! ```
//!
//! # Passing data and/or state to an `on_upgrade` callback
//!
//! ```
//! use axum::{
//! extract::ws::{WebSocketUpgrade, WebSocket},
//! response::Response,
//! routing::get,
//! Extension, Router,
//! };
//!
//! #[derive(Clone)]
//! struct State {
//! // ...
//! }
//!
//! async fn handler(ws: WebSocketUpgrade, Extension(state): Extension<State>) -> Response {
//! ws.on_upgrade(|socket| handle_socket(socket, state))
//! }
//!
//! async fn handle_socket(socket: WebSocket, state: State) {
//! // ...
//! }
//!
//! let app = Router::new()
//! .route("/ws", get(handler))
//! .layer(Extension(State { /* ... */ }));
//! # async {
//! # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
//! # };
//! ```
//!
//! # Read and write concurrently
//!
//! If you need to read and write concurrently from a [`WebSocket`] you can use
Expand Down

0 comments on commit 1ace855

Please sign in to comment.