1
1
//! Module for handling WebSocket connection
2
2
//!
3
- //!
4
3
//! This module provides utilities for setting up and handling WebSocket connections, including
5
4
//! configuring WebSocket options, setting protocols, and upgrading connections.
6
5
//!
7
6
//! It uses [`hyper::upgrade::OnUpgrade`] to upgrade the connection.
8
7
//!
9
- //!
10
8
//! # Example
11
9
//!
12
10
//! ```rust
13
11
//! use futures_util::{SinkExt, StreamExt};
14
12
//! use volo_http::{
15
13
//! response::ServerResponse,
16
14
//! server::{
17
- //! extract ::{Message, WebSocket},
15
+ //! utils ::{Message, WebSocket},
18
16
//! route::get,
19
17
//! utils::WebSocketUpgrade,
20
18
//! },
@@ -63,13 +61,13 @@ pub type WebSocket = WebSocketStream<TokioIo<hyper::upgrade::Upgraded>>;
63
61
/// alias of [`tungstenite::Message`]
64
62
pub type Message = tungstenite:: Message ;
65
63
66
- /// WebSocket headers that will be used for the upgrade request .
64
+ /// WebSocket Request headers for establishing a WebSocket connection .
67
65
struct Headers {
68
66
/// The `Sec-WebSocket-Key` request header value
69
67
/// used for compute 'Sec-WebSocket-Accept' response header value
70
68
sec_websocket_key : HeaderValue ,
71
69
/// The `Sec-WebSocket-Protocol` request header value
72
- /// specify [`Callback`] method depend on protocol
70
+ /// specify [`Callback`] method depend on the protocol
73
71
sec_websocket_protocol : Option < HeaderValue > ,
74
72
}
75
73
@@ -85,7 +83,7 @@ impl std::fmt::Debug for Headers {
85
83
/// WebSocket config
86
84
#[ derive( Default ) ]
87
85
pub struct Config {
88
- /// WebSocket config for transport (alias of [`tungstenite::protocol::WebSocketConfig`] )
86
+ /// WebSocket config for transport (alias of [`WebSocketConfig`]( tungstenite::protocol::WebSocketConfig) )
89
87
/// e.g. max write buffer size
90
88
transport : WebSocketConfig ,
91
89
/// The chosen protocol sent in the `Sec-WebSocket-Protocol` header of the response.
@@ -102,12 +100,12 @@ impl Config {
102
100
}
103
101
}
104
102
105
- /// Set server supported protocols
103
+ /// Set server supported protocols.
104
+ ///
106
105
/// 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
108
107
/// response
109
108
///
110
- ///
111
109
/// ```rust
112
110
/// use volo_http::server::utils::WebSocketConfig;
113
111
///
@@ -160,7 +158,7 @@ impl std::fmt::Debug for Config {
160
158
}
161
159
162
160
/// Callback fn that processes [`WebSocket`]
163
- pub trait Callback : Send + ' static {
161
+ trait Callback : Send + ' static {
164
162
/// Called when a connection upgrade succeeds
165
163
fn call ( self , _: WebSocket ) -> impl Future < Output = ( ) > + Send ;
166
164
}
@@ -224,7 +222,7 @@ impl Callback for DefaultCallback {
224
222
/// # Usage
225
223
///
226
224
/// ```rust
227
- /// use volo_http::{response::ServerResponse, server::extract ::WebSocketUpgrade};
225
+ /// use volo_http::{response::ServerResponse, server::utils ::WebSocketUpgrade};
228
226
///
229
227
/// fn ws_handler(ws: WebSocketUpgrade) -> ServerResponse {
230
228
/// ws.on_upgrade(|socket| unimplemented!())
@@ -256,8 +254,10 @@ where
256
254
/// ```rust
257
255
/// use volo_http::{
258
256
/// response::ServerResponse,
259
- /// server::extract::WebSocketConfig,
260
- /// server::extract::WebSocketUpgrade,
257
+ /// server::utils::{
258
+ /// WebSocketConfig,
259
+ /// WebSocketUpgrade,
260
+ /// }
261
261
/// };
262
262
/// use tokio_tungstenite::tungstenite::protocol::{WebSocketConfig as WebSocketTransConfig};
263
263
///
@@ -284,7 +284,7 @@ where
284
284
/// use std::collections::HashMap;
285
285
/// use volo_http::{
286
286
/// response::ServerResponse,
287
- /// server::extract ::{
287
+ /// server::utils ::{
288
288
/// WebSocketConfig,
289
289
/// WebSocketUpgrade,
290
290
/// WebSocket,
@@ -333,7 +333,7 @@ where
333
333
/// use std::collections::HashMap;
334
334
/// use volo_http::{
335
335
/// response::ServerResponse,
336
- /// server::extract ::{
336
+ /// server::utils ::{
337
337
/// WebSocketConfig,
338
338
/// WebSocketUpgrade,
339
339
/// WebSocket,
@@ -618,7 +618,7 @@ mod websocket_tests {
618
618
async fn on_protocol ( ) {
619
619
use crate :: {
620
620
response:: ServerResponse ,
621
- server:: extract :: { WebSocketConfig , WebSocketUpgrade } ,
621
+ server:: utils :: { WebSocketConfig , WebSocketUpgrade } ,
622
622
} ;
623
623
624
624
async fn ws_handler ( ws : WebSocketUpgrade ) -> ServerResponse {
0 commit comments