Skip to content

Commit 7672b4e

Browse files
committed
feat(http): support websocket server
1 parent 0adfbd6 commit 7672b4e

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

volo-http/src/error/server.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub enum WebSocketUpgradeRejectionError {
122122

123123
impl WebSocketUpgradeRejectionError {
124124
/// Convert the [`WebSocketUpgradeRejectionError`] to the corresponding [`StatusCode`]
125-
pub fn to_status_code(self) -> StatusCode {
125+
fn to_status_code(self) -> StatusCode {
126126
match self {
127127
Self::MethodNotGet => StatusCode::METHOD_NOT_ALLOWED,
128128
Self::InvalidHttpVersion => StatusCode::HTTP_VERSION_NOT_SUPPORTED,

volo-http/src/server/utils/ws.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Module for handling WebSocket connection
1+
//! Handle WebSocket connection
22
//!
33
//! This module provides utilities for setting up and handling WebSocket connections, including
44
//! configuring WebSocket options, setting protocols, and upgrading connections.
@@ -8,6 +8,8 @@
88
//! # Example
99
//!
1010
//! ```rust
11+
//! use std::convert::Infallible;
12+
//!
1113
//! use futures_util::{SinkExt, StreamExt};
1214
//! use volo_http::{
1315
//! response::ServerResponse,
@@ -21,7 +23,7 @@
2123
//! async fn handle_socket(mut socket: WebSocket) {
2224
//! while let Some(Ok(msg)) = socket.next().await {
2325
//! match msg {
24-
//! Message::Text(text) => {
26+
//! Message::Text(_) => {
2527
//! socket.send(msg).await.unwrap();
2628
//! }
2729
//! _ => {}
@@ -33,7 +35,7 @@
3335
//! ws.on_upgrade(handle_socket)
3436
//! }
3537
//!
36-
//! let app = Router::new().route("/ws", get(ws_handler));
38+
//! let app: Router<ServerResponse, Infallible> = Router::new().route("/ws", get(ws_handler));
3739
//! ```
3840
3941
use std::{borrow::Cow, fmt::Formatter, future::Future};
@@ -127,8 +129,8 @@ impl Config {
127129
}
128130

129131
/// Set transport config
130-
/// e.g. write buffer size
131132
///
133+
/// e.g. write buffer size
132134
///
133135
/// ```rust
134136
/// use tokio_tungstenite::tungstenite::protocol::WebSocketConfig as WebSocketTransConfig;
@@ -213,16 +215,18 @@ impl Callback for DefaultCallback {
213215
///
214216
/// **Constrains**:
215217
///
216-
/// The extractor only supports for the request that has the method [`GET`](http::Method::GET)
218+
/// The extractor only supports for the request that has the method [`GET`](http::method::GET)
217219
/// and contains certain header values.
218220
///
221+
/// See more details in [`WebSocketUpgrade::from_context`]
222+
///
219223
/// # Usage
220224
///
221225
/// ```rust
222226
/// use volo_http::{response::ServerResponse, server::utils::WebSocketUpgrade};
223227
///
224228
/// fn ws_handler(ws: WebSocketUpgrade) -> ServerResponse {
225-
/// ws.on_upgrade(|socket| unimplemented!())
229+
/// ws.on_upgrade(|socket| async { unimplemented!() })
226230
/// }
227231
/// ```
228232
pub struct WebSocketUpgrade<F = DefaultOnFailedUpgrade> {

0 commit comments

Comments
 (0)