Skip to content

Commit 152086b

Browse files
committed
feat(http): support websocket server
1 parent f6ab717 commit 152086b

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

volo-http/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ default = []
9090
default_client = ["client", "json"]
9191
default_server = ["server", "query", "form", "json"]
9292

93-
full = ["client", "server", "rustls", "cookie", "query", "form", "json", "tls"]
93+
full = ["client", "server", "rustls", "cookie", "query", "form", "json", "tls", "ws"]
9494

9595
client = ["hyper/client", "hyper/http1"] # client core
9696
server = ["hyper/server", "hyper/http1", "dep:matchit"] # server core

volo-http/src/server/extract.rs

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ use hyper::body::Incoming;
1818
use mime::Mime;
1919
use volo::{context::Context, net::Address};
2020

21-
#[cfg(feature = "ws")]
22-
pub use super::utils::{Message, WebSocket, WebSocketConfig, WebSocketUpgrade};
2321
use super::IntoResponse;
2422
use crate::{
2523
context::ServerContext,

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

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
//! Module for handling WebSocket connection
22
//!
3-
//!
43
//! This module provides utilities for setting up and handling WebSocket connections, including
54
//! configuring WebSocket options, setting protocols, and upgrading connections.
65
//!
76
//! It uses [`hyper::upgrade::OnUpgrade`] to upgrade the connection.
87
//!
9-
//!
108
//! # Example
119
//!
1210
//! ```rust
1311
//! use futures_util::{SinkExt, StreamExt};
1412
//! use volo_http::{
1513
//! response::ServerResponse,
1614
//! server::{
17-
//! extract::{Message, WebSocket},
15+
//! utils::{Message, WebSocket},
1816
//! route::get,
1917
//! utils::WebSocketUpgrade,
2018
//! },
@@ -63,13 +61,13 @@ pub type WebSocket = WebSocketStream<TokioIo<hyper::upgrade::Upgraded>>;
6361
/// alias of [`tungstenite::Message`]
6462
pub type Message = tungstenite::Message;
6563

66-
/// WebSocket headers that will be used for the upgrade request.
64+
/// WebSocket Request headers for establishing a WebSocket connection.
6765
struct Headers {
6866
/// The `Sec-WebSocket-Key` request header value
6967
/// used for compute 'Sec-WebSocket-Accept' response header value
7068
sec_websocket_key: HeaderValue,
7169
/// The `Sec-WebSocket-Protocol` request header value
72-
/// specify [`Callback`] method depend on protocol
70+
/// specify [`Callback`] method depend on the protocol
7371
sec_websocket_protocol: Option<HeaderValue>,
7472
}
7573

@@ -85,7 +83,7 @@ impl std::fmt::Debug for Headers {
8583
/// WebSocket config
8684
#[derive(Default)]
8785
pub struct Config {
88-
/// WebSocket config for transport (alias of [`tungstenite::protocol::WebSocketConfig`])
86+
/// WebSocket config for transport (alias of [`WebSocketConfig`](tungstenite::protocol::WebSocketConfig))
8987
/// e.g. max write buffer size
9088
transport: WebSocketConfig,
9189
/// The chosen protocol sent in the `Sec-WebSocket-Protocol` header of the response.
@@ -102,12 +100,12 @@ impl Config {
102100
}
103101
}
104102

105-
/// Set server supported protocols
103+
/// Set server supported protocols.
104+
///
106105
/// This will filter protocols in request header `Sec-WebSocket-Protocol`
107-
/// will set the first server supported protocol in [`http::header::Sec-WebSocket-Protocol`] in
106+
/// and will set the first server supported protocol in [`http::header::Sec-WebSocket-Protocol`] in
108107
/// response
109108
///
110-
///
111109
/// ```rust
112110
/// use volo_http::server::utils::WebSocketConfig;
113111
///
@@ -160,7 +158,7 @@ impl std::fmt::Debug for Config {
160158
}
161159

162160
/// Callback fn that processes [`WebSocket`]
163-
pub trait Callback: Send + 'static {
161+
trait Callback: Send + 'static {
164162
/// Called when a connection upgrade succeeds
165163
fn call(self, _: WebSocket) -> impl Future<Output = ()> + Send;
166164
}
@@ -224,7 +222,7 @@ impl Callback for DefaultCallback {
224222
/// # Usage
225223
///
226224
/// ```rust
227-
/// use volo_http::{response::ServerResponse, server::extract::WebSocketUpgrade};
225+
/// use volo_http::{response::ServerResponse, server::utils::WebSocketUpgrade};
228226
///
229227
/// fn ws_handler(ws: WebSocketUpgrade) -> ServerResponse {
230228
/// ws.on_upgrade(|socket| unimplemented!())
@@ -256,8 +254,10 @@ where
256254
/// ```rust
257255
/// use volo_http::{
258256
/// response::ServerResponse,
259-
/// server::extract::WebSocketConfig,
260-
/// server::extract::WebSocketUpgrade,
257+
/// server::utils::{
258+
/// WebSocketConfig,
259+
/// WebSocketUpgrade,
260+
/// }
261261
/// };
262262
/// use tokio_tungstenite::tungstenite::protocol::{WebSocketConfig as WebSocketTransConfig};
263263
///
@@ -284,7 +284,7 @@ where
284284
/// use std::collections::HashMap;
285285
/// use volo_http::{
286286
/// response::ServerResponse,
287-
/// server::extract::{
287+
/// server::utils::{
288288
/// WebSocketConfig,
289289
/// WebSocketUpgrade,
290290
/// WebSocket,
@@ -333,7 +333,7 @@ where
333333
/// use std::collections::HashMap;
334334
/// use volo_http::{
335335
/// response::ServerResponse,
336-
/// server::extract::{
336+
/// server::utils::{
337337
/// WebSocketConfig,
338338
/// WebSocketUpgrade,
339339
/// WebSocket,
@@ -618,7 +618,7 @@ mod websocket_tests {
618618
async fn on_protocol() {
619619
use crate::{
620620
response::ServerResponse,
621-
server::extract::{WebSocketConfig, WebSocketUpgrade},
621+
server::utils::{WebSocketConfig, WebSocketUpgrade},
622622
};
623623

624624
async fn ws_handler(ws: WebSocketUpgrade) -> ServerResponse {

0 commit comments

Comments
 (0)