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
+6 -4
View File
@@ -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
{
+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
{