Skip to content

Commit

Permalink
feat(http): support websocket server
Browse files Browse the repository at this point in the history
  • Loading branch information
StellarisW committed Aug 7, 2024
1 parent f6ab717 commit 152086b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion volo-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ default = []
default_client = ["client", "json"]
default_server = ["server", "query", "form", "json"]

full = ["client", "server", "rustls", "cookie", "query", "form", "json", "tls"]
full = ["client", "server", "rustls", "cookie", "query", "form", "json", "tls", "ws"]

client = ["hyper/client", "hyper/http1"] # client core
server = ["hyper/server", "hyper/http1", "dep:matchit"] # server core
Expand Down
2 changes: 0 additions & 2 deletions volo-http/src/server/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ use hyper::body::Incoming;
use mime::Mime;
use volo::{context::Context, net::Address};

#[cfg(feature = "ws")]
pub use super::utils::{Message, WebSocket, WebSocketConfig, WebSocketUpgrade};
use super::IntoResponse;
use crate::{
context::ServerContext,
Expand Down
32 changes: 16 additions & 16 deletions volo-http/src/server/utils/ws.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
//! Module for handling WebSocket connection
//!
//!
//! This module provides utilities for setting up and handling WebSocket connections, including
//! configuring WebSocket options, setting protocols, and upgrading connections.
//!
//! It uses [`hyper::upgrade::OnUpgrade`] to upgrade the connection.
//!
//!
//! # Example
//!
//! ```rust
//! use futures_util::{SinkExt, StreamExt};
//! use volo_http::{
//! response::ServerResponse,
//! server::{
//! extract::{Message, WebSocket},
//! utils::{Message, WebSocket},
//! route::get,
//! utils::WebSocketUpgrade,
//! },
Expand Down Expand Up @@ -63,13 +61,13 @@ pub type WebSocket = WebSocketStream<TokioIo<hyper::upgrade::Upgraded>>;
/// alias of [`tungstenite::Message`]
pub type Message = tungstenite::Message;

/// WebSocket headers that will be used for the upgrade request.
/// WebSocket Request headers for establishing a WebSocket connection.
struct Headers {
/// The `Sec-WebSocket-Key` request header value
/// used for compute 'Sec-WebSocket-Accept' response header value
sec_websocket_key: HeaderValue,
/// The `Sec-WebSocket-Protocol` request header value
/// specify [`Callback`] method depend on protocol
/// specify [`Callback`] method depend on the protocol
sec_websocket_protocol: Option<HeaderValue>,
}

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

/// Set server supported protocols
/// Set server supported protocols.
///
/// This will filter protocols in request header `Sec-WebSocket-Protocol`
/// will set the first server supported protocol in [`http::header::Sec-WebSocket-Protocol`] in
/// and will set the first server supported protocol in [`http::header::Sec-WebSocket-Protocol`] in
/// response
///
///
/// ```rust
/// use volo_http::server::utils::WebSocketConfig;
///
Expand Down Expand Up @@ -160,7 +158,7 @@ impl std::fmt::Debug for Config {
}

/// Callback fn that processes [`WebSocket`]
pub trait Callback: Send + 'static {
trait Callback: Send + 'static {
/// Called when a connection upgrade succeeds
fn call(self, _: WebSocket) -> impl Future<Output = ()> + Send;
}
Expand Down Expand Up @@ -224,7 +222,7 @@ impl Callback for DefaultCallback {
/// # Usage
///
/// ```rust
/// use volo_http::{response::ServerResponse, server::extract::WebSocketUpgrade};
/// use volo_http::{response::ServerResponse, server::utils::WebSocketUpgrade};
///
/// fn ws_handler(ws: WebSocketUpgrade) -> ServerResponse {
/// ws.on_upgrade(|socket| unimplemented!())
Expand Down Expand Up @@ -256,8 +254,10 @@ where
/// ```rust
/// use volo_http::{
/// response::ServerResponse,
/// server::extract::WebSocketConfig,
/// server::extract::WebSocketUpgrade,
/// server::utils::{
/// WebSocketConfig,
/// WebSocketUpgrade,
/// }
/// };
/// use tokio_tungstenite::tungstenite::protocol::{WebSocketConfig as WebSocketTransConfig};
///
Expand All @@ -284,7 +284,7 @@ where
/// use std::collections::HashMap;
/// use volo_http::{
/// response::ServerResponse,
/// server::extract::{
/// server::utils::{
/// WebSocketConfig,
/// WebSocketUpgrade,
/// WebSocket,
Expand Down Expand Up @@ -333,7 +333,7 @@ where
/// use std::collections::HashMap;
/// use volo_http::{
/// response::ServerResponse,
/// server::extract::{
/// server::utils::{
/// WebSocketConfig,
/// WebSocketUpgrade,
/// WebSocket,
Expand Down Expand Up @@ -618,7 +618,7 @@ mod websocket_tests {
async fn on_protocol() {
use crate::{
response::ServerResponse,
server::extract::{WebSocketConfig, WebSocketUpgrade},
server::utils::{WebSocketConfig, WebSocketUpgrade},
};

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

0 comments on commit 152086b

Please sign in to comment.