Module axum::extract::ws [−][src]
This is supported on crate feature
ws
only.Expand description
Handle WebSocket connections.
Example
use axum::{
extract::ws::{WebSocketUpgrade, WebSocket},
handler::get,
response::IntoResponse,
Router,
};
let app = Router::new().route("/ws", get(handler));
async fn handler(ws: WebSocketUpgrade) -> impl IntoResponse {
ws.on_upgrade(handle_socket)
}
async fn handle_socket(mut socket: WebSocket) {
while let Some(msg) = socket.recv().await {
let msg = if let Ok(msg) = msg {
msg
} else {
// client disconnected
return;
};
if socket.send(msg).await.is_err() {
// client disconnected
return;
}
}
}
Modules
WebSocket specific rejections.
Structs
A struct representing the close command.
A stream of WebSocket messages.
Extractor for establishing WebSocket connections.
Enums
A WebSocket message.
Type Definitions
Status code used to indicate why an endpoint is closing the WebSocket connection.