Upgrade tokio-tungstenite to 0.26 (#3078)

This commit is contained in:
Lena
2024-12-18 15:15:55 -05:00
committed by GitHub
parent 5cdd8a4f18
commit 96e071c8fb
9 changed files with 194 additions and 58 deletions
+10 -6
View File
@@ -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
{