mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-22 00:00:17 +02:00
Upgrade tokio-tungstenite to 0.26 (#3078)
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
|
||||
use axum::{
|
||||
extract::{
|
||||
ws::{Message, WebSocket, WebSocketUpgrade},
|
||||
ws::{Message, Utf8Bytes, WebSocket, WebSocketUpgrade},
|
||||
State,
|
||||
},
|
||||
response::{Html, IntoResponse},
|
||||
@@ -79,7 +79,7 @@ async fn websocket(stream: WebSocket, state: Arc<AppState>) {
|
||||
while let Some(Ok(message)) = receiver.next().await {
|
||||
if let Message::Text(name) = message {
|
||||
// If username that is sent by client is not taken, fill username string.
|
||||
check_username(&state, &mut username, &name);
|
||||
check_username(&state, &mut username, name.as_str());
|
||||
|
||||
// If not empty we want to quit the loop else we want to quit function.
|
||||
if !username.is_empty() {
|
||||
@@ -87,7 +87,9 @@ async fn websocket(stream: WebSocket, state: Arc<AppState>) {
|
||||
} else {
|
||||
// Only send our client that username is taken.
|
||||
let _ = sender
|
||||
.send(Message::Text(String::from("Username already taken.")))
|
||||
.send(Message::Text(Utf8Bytes::from_static(
|
||||
"Username already taken.",
|
||||
)))
|
||||
.await;
|
||||
|
||||
return;
|
||||
@@ -109,7 +111,7 @@ async fn websocket(stream: WebSocket, state: Arc<AppState>) {
|
||||
let mut send_task = tokio::spawn(async move {
|
||||
while let Ok(msg) = rx.recv().await {
|
||||
// In any websocket error, break loop.
|
||||
if sender.send(Message::Text(msg)).await.is_err() {
|
||||
if sender.send(Message::text(msg)).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,4 +8,4 @@ publish = false
|
||||
axum = { path = "../../axum", features = ["ws"] }
|
||||
futures = "0.3"
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
tokio-tungstenite = "0.24"
|
||||
tokio-tungstenite = "0.26"
|
||||
|
||||
@@ -48,7 +48,7 @@ async fn integration_testable_handle_socket(mut socket: WebSocket) {
|
||||
while let Some(Ok(msg)) = socket.recv().await {
|
||||
if let Message::Text(msg) = msg {
|
||||
if socket
|
||||
.send(Message::Text(format!("You said: {msg}")))
|
||||
.send(Message::Text(format!("You said: {msg}").into()))
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
@@ -79,7 +79,7 @@ where
|
||||
while let Some(Ok(msg)) = read.next().await {
|
||||
if let Message::Text(msg) = msg {
|
||||
if write
|
||||
.send(Message::Text(format!("You said: {msg}")))
|
||||
.send(Message::Text(format!("You said: {msg}").into()))
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
@@ -123,7 +123,7 @@ mod tests {
|
||||
other => panic!("expected a text message but got {other:?}"),
|
||||
};
|
||||
|
||||
assert_eq!(msg, "You said: foo");
|
||||
assert_eq!(msg.as_str(), "You said: foo");
|
||||
}
|
||||
|
||||
// We can unit test the other handler by creating channels to read and write from.
|
||||
@@ -136,16 +136,13 @@ mod tests {
|
||||
|
||||
tokio::spawn(unit_testable_handle_socket(socket_write, socket_read));
|
||||
|
||||
test_tx
|
||||
.send(Ok(Message::Text("foo".to_owned())))
|
||||
.await
|
||||
.unwrap();
|
||||
test_tx.send(Ok(Message::Text("foo".into()))).await.unwrap();
|
||||
|
||||
let msg = match test_rx.next().await.unwrap() {
|
||||
Message::Text(msg) => msg,
|
||||
other => panic!("expected a text message but got {other:?}"),
|
||||
};
|
||||
|
||||
assert_eq!(msg, "You said: foo");
|
||||
assert_eq!(msg.as_str(), "You said: foo");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ async fn ws_handler(
|
||||
res = ws.recv() => {
|
||||
match res {
|
||||
Some(Ok(ws::Message::Text(s))) => {
|
||||
let _ = sender.send(s);
|
||||
let _ = sender.send(s.to_string());
|
||||
}
|
||||
Some(Ok(_)) => {}
|
||||
Some(Err(e)) => tracing::debug!("client disconnected abruptly: {e}"),
|
||||
@@ -85,7 +85,7 @@ async fn ws_handler(
|
||||
// Tokio guarantees that `broadcast::Receiver::recv` is cancel-safe.
|
||||
res = receiver.recv() => {
|
||||
match res {
|
||||
Ok(msg) => if let Err(e) = ws.send(ws::Message::Text(msg)).await {
|
||||
Ok(msg) => if let Err(e) = ws.send(ws::Message::Text(msg.into())).await {
|
||||
tracing::debug!("client disconnected abruptly: {e}");
|
||||
}
|
||||
Err(_) => continue,
|
||||
|
||||
@@ -11,7 +11,7 @@ futures = "0.3"
|
||||
futures-util = { version = "0.3", default-features = false, features = ["sink", "std"] }
|
||||
headers = "0.4"
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
tokio-tungstenite = "0.24.0"
|
||||
tokio-tungstenite = "0.26.0"
|
||||
tower-http = { version = "0.6.1", features = ["fs", "trace"] }
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
|
||||
@@ -12,9 +12,9 @@
|
||||
|
||||
use futures_util::stream::FuturesUnordered;
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use std::borrow::Cow;
|
||||
use std::ops::ControlFlow;
|
||||
use std::time::Instant;
|
||||
use tokio_tungstenite::tungstenite::Utf8Bytes;
|
||||
|
||||
// we will use tungstenite for websocket client impl (same library as what axum is using)
|
||||
use tokio_tungstenite::{
|
||||
@@ -65,7 +65,9 @@ async fn spawn_client(who: usize) {
|
||||
|
||||
//we can ping the server for start
|
||||
sender
|
||||
.send(Message::Ping("Hello, Server!".into()))
|
||||
.send(Message::Ping(axum::body::Bytes::from_static(
|
||||
b"Hello, Server!",
|
||||
)))
|
||||
.await
|
||||
.expect("Can not send!");
|
||||
|
||||
@@ -74,7 +76,7 @@ async fn spawn_client(who: usize) {
|
||||
for i in 1..30 {
|
||||
// In any websocket error, break loop.
|
||||
if sender
|
||||
.send(Message::Text(format!("Message number {i}...")))
|
||||
.send(Message::Text(format!("Message number {i}...").into()))
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
@@ -90,7 +92,7 @@ async fn spawn_client(who: usize) {
|
||||
if let Err(e) = sender
|
||||
.send(Message::Close(Some(CloseFrame {
|
||||
code: CloseCode::Normal,
|
||||
reason: Cow::from("Goodbye"),
|
||||
reason: Utf8Bytes::from_static("Goodbye"),
|
||||
})))
|
||||
.await
|
||||
{
|
||||
|
||||
@@ -17,14 +17,14 @@
|
||||
//! ```
|
||||
|
||||
use axum::{
|
||||
extract::ws::{Message, WebSocket, WebSocketUpgrade},
|
||||
body::Bytes,
|
||||
extract::ws::{Message, Utf8Bytes, WebSocket, WebSocketUpgrade},
|
||||
response::IntoResponse,
|
||||
routing::any,
|
||||
Router,
|
||||
};
|
||||
use axum_extra::TypedHeader;
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::ops::ControlFlow;
|
||||
use std::{net::SocketAddr, path::PathBuf};
|
||||
use tower_http::{
|
||||
@@ -101,7 +101,11 @@ async fn ws_handler(
|
||||
/// Actual websocket statemachine (one will be spawned per connection)
|
||||
async fn handle_socket(mut socket: WebSocket, who: SocketAddr) {
|
||||
// send a ping (unsupported by some browsers) just to kick things off and get a response
|
||||
if socket.send(Message::Ping(vec![1, 2, 3])).await.is_ok() {
|
||||
if socket
|
||||
.send(Message::Ping(Bytes::from_static(&[1, 2, 3])))
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
println!("Pinged {who}...");
|
||||
} else {
|
||||
println!("Could not send ping {who}!");
|
||||
@@ -131,7 +135,7 @@ async fn handle_socket(mut socket: WebSocket, who: SocketAddr) {
|
||||
// connecting to server and receiving their greetings.
|
||||
for i in 1..5 {
|
||||
if socket
|
||||
.send(Message::Text(format!("Hi {i} times!")))
|
||||
.send(Message::Text(format!("Hi {i} times!").into()))
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
@@ -151,7 +155,7 @@ async fn handle_socket(mut socket: WebSocket, who: SocketAddr) {
|
||||
for i in 0..n_msg {
|
||||
// In case of any websocket error, we exit.
|
||||
if sender
|
||||
.send(Message::Text(format!("Server message {i} ...")))
|
||||
.send(Message::Text(format!("Server message {i} ...").into()))
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
@@ -165,7 +169,7 @@ async fn handle_socket(mut socket: WebSocket, who: SocketAddr) {
|
||||
if let Err(e) = sender
|
||||
.send(Message::Close(Some(CloseFrame {
|
||||
code: axum::extract::ws::close_code::NORMAL,
|
||||
reason: Cow::from("Goodbye"),
|
||||
reason: Utf8Bytes::from_static("Goodbye"),
|
||||
})))
|
||||
.await
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user