diff --git a/Cargo.toml b/Cargo.toml
index e96312588..a5650cbee 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -124,7 +124,7 @@ tokio-native-tls = "0.3"
tokio-rustls = {version = "0.26", default-features = false }
tokio-openssl = "0.6"
tokio-stream = { version = "0.1", default-features = false }
-tokio-tungstenite = { version = "0.24", default-features = false }
+tokio-tungstenite = { version = "0.25", default-features = false }
tokio-util = "0.7"
tower = { version = "0.5", default-features = false }
tracing-subscriber = { version = "0.3" }
diff --git a/crates/extra/src/websocket.rs b/crates/extra/src/websocket.rs
index de9676ac7..fcf4ead7b 100644
--- a/crates/extra/src/websocket.rs
+++ b/crates/extra/src/websocket.rs
@@ -29,7 +29,7 @@
//! // client disconnected
//! return;
//! };
-//!
+//!
//! if ws.send(msg).await.is_err() {
//! // client disconnected
//! return;
@@ -47,11 +47,11 @@
//! #[tokio::main]
//! async fn main() {
//! let router = Router::new().get(index).push(Router::with_path("ws").goal(connect));
-//!
+//!
//! let acceptor = TcpListener::new("0.0.0.0:5800").bind().await;
//! Server::new(acceptor).serve(router).await;
//! }
-//!
+//!
//! static INDEX_HTML: &str = r#"
//!
//!
@@ -81,21 +81,22 @@ use std::borrow::Cow;
use std::fmt::{self, Debug, Formatter};
use std::future::Future;
use std::pin::Pin;
-use std::task::{Context, Poll, ready};
+use std::task::{ready, Context, Poll};
use futures_util::sink::{Sink, SinkExt};
use futures_util::stream::{Stream, StreamExt};
use futures_util::{future, FutureExt, TryFutureExt};
use hyper::upgrade::OnUpgrade;
use salvo_core::http::header::{SEC_WEBSOCKET_VERSION, UPGRADE};
-use salvo_core::http::headers::{Connection, HeaderMapExt, SecWebsocketAccept, SecWebsocketKey, Upgrade};
+use salvo_core::http::headers::{
+ Connection, HeaderMapExt, SecWebsocketAccept, SecWebsocketKey, Upgrade,
+};
use salvo_core::http::{StatusCode, StatusError};
use salvo_core::rt::tokio::TokioIo;
use salvo_core::{Error, Request, Response};
-use tokio_tungstenite::{
- tungstenite::protocol::{self, WebSocketConfig},
- WebSocketStream,
-};
+use tokio_tungstenite::tungstenite::protocol::frame::{Payload, Utf8Payload};
+use tokio_tungstenite::tungstenite::protocol::{self, WebSocketConfig};
+use tokio_tungstenite::WebSocketStream;
/// Creates a WebSocket Handler.
/// Request:
@@ -132,7 +133,9 @@ impl WebSocketUpgrade {
/// Create new `WebSocketUpgrade` with config.
#[inline]
pub fn with_config(config: WebSocketConfig) -> Self {
- WebSocketUpgrade { config: Some(config) }
+ WebSocketUpgrade {
+ config: Some(config),
+ }
}
/// The target minimum size of the write buffer to reach before writing the data
@@ -143,7 +146,9 @@ impl WebSocketUpgrade {
/// It is often more optimal to allow them to buffer a little, hence the default value.
#[inline]
pub fn write_buffer_size(mut self, max: usize) -> Self {
- self.config.get_or_insert_with(WebSocketConfig::default).write_buffer_size = max;
+ self.config
+ .get_or_insert_with(WebSocketConfig::default)
+ .write_buffer_size = max;
self
}
@@ -159,7 +164,9 @@ impl WebSocketUpgrade {
/// and probably a little more depending on error handling strategy.
#[inline]
pub fn max_write_buffer_size(mut self, max: usize) -> Self {
- self.config.get_or_insert_with(WebSocketConfig::default).max_write_buffer_size = max;
+ self.config
+ .get_or_insert_with(WebSocketConfig::default)
+ .max_write_buffer_size = max;
self
}
@@ -180,7 +187,9 @@ impl WebSocketUpgrade {
/// by a malicious user.
#[inline]
pub fn max_frame_size(mut self, max: usize) -> Self {
- self.config.get_or_insert_with(WebSocketConfig::default).max_frame_size = Some(max);
+ self.config
+ .get_or_insert_with(WebSocketConfig::default)
+ .max_frame_size = Some(max);
self
}
@@ -191,13 +200,19 @@ impl WebSocketUpgrade {
/// By default this option is set to `false`, i.e. according to RFC 6455.
#[inline]
pub fn accept_unmasked_frames(mut self, accept: bool) -> Self {
- self.config.get_or_insert_with(WebSocketConfig::default).accept_unmasked_frames = accept;
+ self.config
+ .get_or_insert_with(WebSocketConfig::default)
+ .accept_unmasked_frames = accept;
self
}
-
/// Upgrade websocket request.
- pub async fn upgrade(&self, req: &mut Request, res: &mut Response, callback: F) -> Result<(), StatusError>
+ pub async fn upgrade(
+ &self,
+ req: &mut Request,
+ res: &mut Response,
+ callback: F,
+ ) -> Result<(), StatusError>
where
F: FnOnce(WebSocket) -> Fut + Send + 'static,
Fut: Future