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 8, 2024
1 parent 0adfbd6 commit 7672b4e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion volo-http/src/error/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub enum WebSocketUpgradeRejectionError {

impl WebSocketUpgradeRejectionError {
/// Convert the [`WebSocketUpgradeRejectionError`] to the corresponding [`StatusCode`]
pub fn to_status_code(self) -> StatusCode {
fn to_status_code(self) -> StatusCode {

Check warning on line 125 in volo-http/src/error/server.rs

View workflow job for this annotation

GitHub Actions / clippy

methods with the following characteristics: (`to_*` and `self` type is not `Copy`) usually take `self` by reference

warning: methods with the following characteristics: (`to_*` and `self` type is not `Copy`) usually take `self` by reference --> volo-http/src/error/server.rs:125:23 | 125 | fn to_status_code(self) -> StatusCode { | ^^^^ | = help: consider choosing a less ambiguous name = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#wrong_self_convention = note: `#[warn(clippy::wrong_self_convention)]` on by default
match self {
Self::MethodNotGet => StatusCode::METHOD_NOT_ALLOWED,
Self::InvalidHttpVersion => StatusCode::HTTP_VERSION_NOT_SUPPORTED,
Expand Down
16 changes: 10 additions & 6 deletions volo-http/src/server/utils/ws.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Module for handling WebSocket connection
//! Handle WebSocket connection
//!
//! This module provides utilities for setting up and handling WebSocket connections, including
//! configuring WebSocket options, setting protocols, and upgrading connections.
Expand All @@ -8,6 +8,8 @@
//! # Example
//!
//! ```rust
//! use std::convert::Infallible;
//!
//! use futures_util::{SinkExt, StreamExt};
//! use volo_http::{
//! response::ServerResponse,
Expand All @@ -21,7 +23,7 @@
//! async fn handle_socket(mut socket: WebSocket) {
//! while let Some(Ok(msg)) = socket.next().await {
//! match msg {
//! Message::Text(text) => {
//! Message::Text(_) => {
//! socket.send(msg).await.unwrap();
//! }
//! _ => {}
Expand All @@ -33,7 +35,7 @@
//! ws.on_upgrade(handle_socket)
//! }
//!
//! let app = Router::new().route("/ws", get(ws_handler));
//! let app: Router<ServerResponse, Infallible> = Router::new().route("/ws", get(ws_handler));
//! ```

use std::{borrow::Cow, fmt::Formatter, future::Future};
Expand Down Expand Up @@ -127,8 +129,8 @@ impl Config {
}

/// Set transport config
/// e.g. write buffer size
///
/// e.g. write buffer size
///
/// ```rust
/// use tokio_tungstenite::tungstenite::protocol::WebSocketConfig as WebSocketTransConfig;
Expand Down Expand Up @@ -213,16 +215,18 @@ impl Callback for DefaultCallback {
///
/// **Constrains**:
///
/// The extractor only supports for the request that has the method [`GET`](http::Method::GET)
/// The extractor only supports for the request that has the method [`GET`](http::method::GET)
/// and contains certain header values.
///
/// See more details in [`WebSocketUpgrade::from_context`]
///
/// # Usage
///
/// ```rust
/// use volo_http::{response::ServerResponse, server::utils::WebSocketUpgrade};
///
/// fn ws_handler(ws: WebSocketUpgrade) -> ServerResponse {
/// ws.on_upgrade(|socket| unimplemented!())
/// ws.on_upgrade(|socket| async { unimplemented!() })
/// }
/// ```
pub struct WebSocketUpgrade<F = DefaultOnFailedUpgrade> {
Expand Down

0 comments on commit 7672b4e

Please sign in to comment.