Skip to content
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

Remove .close(), and document clean WebSocket closing #2974

Merged
merged 5 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions axum/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
and `MethodRouter::connect[_service]` ([#2961])
- **fixed:** Avoid setting `content-length` before middleware ([#2897]).
This allows middleware to add bodies to requests without needing to manually set `content-length`
- **breaking:** Remove `WebSocket::close` ([#2974]).
Users should explicitly send close messages themselves.

[#2897]: https://github.com/tokio-rs/axum/pull/2897
[#2984]: https://github.com/tokio-rs/axum/pull/2984
[#2961]: https://github.com/tokio-rs/axum/pull/2961
[#2974]: https://github.com/tokio-rs/axum/pull/2974

# 0.8.0

Expand Down
23 changes: 18 additions & 5 deletions axum/src/extract/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,6 @@ impl WebSocket {
.map_err(Error::new)
}

/// Gracefully close this WebSocket.
pub async fn close(mut self) -> Result<(), Error> {
self.inner.close(None).await.map_err(Error::new)
}

/// Return the selected WebSocket subprotocol, if one has been chosen.
pub fn protocol(&self) -> Option<&HeaderValue> {
self.protocol.as_ref()
Expand Down Expand Up @@ -615,6 +610,24 @@ pub enum Message {
/// [unidirectional heartbeat](https://tools.ietf.org/html/rfc6455#section-5.5.3).
Pong(Vec<u8>),
/// A close message with the optional close frame.
///
/// You may "uncleanly" close a WebSocket connection at any time
/// by simply dropping the [`WebSocket`].
/// However, you may also use the graceful closing protocol, in which
/// 1. peer A sends a close frame, and does not send any further messages;
/// 2. peer B responds with a close frame, and does not send any further messages;
/// 3. peer A processes the remaining messages sent by peer B, before finally
/// 4. both peers close the connection.
///
/// After sending a close frame,
/// you may still read messages,
/// but attempts to send another message will error.
/// After receiving a close frame,
/// Axum will automatically respond with a close frame if necessary
jplatte marked this conversation as resolved.
Show resolved Hide resolved
/// (you do not have to deal with this yourself).
/// Since no further messages will be received,
/// you may either do nothing
/// or explicitly drop the connection.
Close(Option<CloseFrame<'static>>),
}

Expand Down