Skip to content

Commit

Permalink
Remove .close(), and document clean WebSocket closing (#2974)
Browse files Browse the repository at this point in the history
  • Loading branch information
SabrinaJewson authored Oct 11, 2024
1 parent a0f310a commit c6e6b4e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
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
/// (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

0 comments on commit c6e6b4e

Please sign in to comment.