Skip to content

Commit c6e6b4e

Browse files
Remove .close(), and document clean WebSocket closing (#2974)
1 parent a0f310a commit c6e6b4e

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

axum/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
and `MethodRouter::connect[_service]` ([#2961])
1414
- **fixed:** Avoid setting `content-length` before middleware ([#2897]).
1515
This allows middleware to add bodies to requests without needing to manually set `content-length`
16+
- **breaking:** Remove `WebSocket::close` ([#2974]).
17+
Users should explicitly send close messages themselves.
1618

1719
[#2897]: https://github.com/tokio-rs/axum/pull/2897
1820
[#2984]: https://github.com/tokio-rs/axum/pull/2984
1921
[#2961]: https://github.com/tokio-rs/axum/pull/2961
22+
[#2974]: https://github.com/tokio-rs/axum/pull/2974
2023

2124
# 0.8.0
2225

axum/src/extract/ws.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,6 @@ impl WebSocket {
507507
.map_err(Error::new)
508508
}
509509

510-
/// Gracefully close this WebSocket.
511-
pub async fn close(mut self) -> Result<(), Error> {
512-
self.inner.close(None).await.map_err(Error::new)
513-
}
514-
515510
/// Return the selected WebSocket subprotocol, if one has been chosen.
516511
pub fn protocol(&self) -> Option<&HeaderValue> {
517512
self.protocol.as_ref()
@@ -615,6 +610,24 @@ pub enum Message {
615610
/// [unidirectional heartbeat](https://tools.ietf.org/html/rfc6455#section-5.5.3).
616611
Pong(Vec<u8>),
617612
/// A close message with the optional close frame.
613+
///
614+
/// You may "uncleanly" close a WebSocket connection at any time
615+
/// by simply dropping the [`WebSocket`].
616+
/// However, you may also use the graceful closing protocol, in which
617+
/// 1. peer A sends a close frame, and does not send any further messages;
618+
/// 2. peer B responds with a close frame, and does not send any further messages;
619+
/// 3. peer A processes the remaining messages sent by peer B, before finally
620+
/// 4. both peers close the connection.
621+
///
622+
/// After sending a close frame,
623+
/// you may still read messages,
624+
/// but attempts to send another message will error.
625+
/// After receiving a close frame,
626+
/// axum will automatically respond with a close frame if necessary
627+
/// (you do not have to deal with this yourself).
628+
/// Since no further messages will be received,
629+
/// you may either do nothing
630+
/// or explicitly drop the connection.
618631
Close(Option<CloseFrame<'static>>),
619632
}
620633

0 commit comments

Comments
 (0)