-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bring websocket handling more inline with handler API #111
Labels
C-enhancement
Category: A PR with an enhancement
Milestone
Comments
davidpdrsn
added a commit
that referenced
this issue
Aug 4, 2021
Fixes #111 Example usage: ```rust use axum::{ prelude::*, extract::ws::{WebSocketUpgrade, WebSocket}, response::IntoResponse, }; let app = 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; } } } ```
davidpdrsn
added a commit
that referenced
this issue
Aug 4, 2021
Fixes #111 Example usage: ```rust use axum::{ prelude::*, extract::ws::{WebSocketUpgrade, WebSocket}, response::IntoResponse, }; let app = 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; } } } ```
davidpdrsn
added a commit
that referenced
this issue
Aug 7, 2021
Fixes #111 Example usage: ```rust use axum::{ prelude::*, extract::ws::{WebSocketUpgrade, WebSocket}, response::IntoResponse, }; let app = 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; } } } ```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feature Request
Motivation
Currently handling websockets works quite differently from handling other requests. This has a few downsides:
WebSocketHandler::call
should be allowed to fail #73.handler/mod.rs
.Proposal
Change the websocket API to instead use a regular handler and a special
IntoResponse
. Something likeThis is similar to how things work in actix.
This solution was suggest by
gbaranski#5119
on Discord.Alternatives
Leads to slightly more code in the basic case where you don't wanna apply any extractors and just wanna start a ws connection without rejections. I think that tradeoff is worth it though.
The text was updated successfully, but these errors were encountered: