Skip to content

Commit

Permalink
Revert update to tunstenite 0.23
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Oct 10, 2024
1 parent f396ba0 commit 93a78df
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 25 deletions.
16 changes: 9 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ futures-util = { version = "0.3", default-features = false }
js-sys = "0.3"
log = "0.4"
tokio = "1.16"
tokio-tungstenite = "0.23"
tungstenite = "0.23"
tokio-tungstenite = ">=0.17, <=0.21"
tungstenite = { version = ">=0.17, <=0.21" }
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
web-sys = "0.3"
Expand Down
5 changes: 3 additions & 2 deletions echo_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(deprecated)] // TODO(emilk): Remove when we update tungstenite
#![allow(clippy::unwrap_used, clippy::disallowed_methods)] // We are just testing here.

use std::{net::TcpListener, thread::spawn};
Expand All @@ -10,10 +11,10 @@ fn main() {
spawn(move || {
let mut websocket = tungstenite::accept(stream.unwrap()).unwrap();
eprintln!("New client connected");
while let Ok(msg) = websocket.read() {
while let Ok(msg) = websocket.read_message() {
// We do not want to send back ping/pong messages.
if msg.is_binary() || msg.is_text() {
if let Err(err) = websocket.send(msg) {
if let Err(err) = websocket.write_message(msg) {
eprintln!("Error sending message: {err}");
break;
} else {
Expand Down
4 changes: 0 additions & 4 deletions ewebsock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,12 @@ pub struct Options {
///
/// Ignored on Web.
pub max_incoming_frame_size: usize,

/// Delay blocking in ms - default 10ms
pub delay_blocking: std::time::Duration,
}

impl Default for Options {
fn default() -> Self {
Self {
max_incoming_frame_size: 64 * 1024 * 1024,
delay_blocking: std::time::Duration::from_millis(10),
}
}
}
Expand Down
17 changes: 8 additions & 9 deletions ewebsock/src/native_tungstenite.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Native implementation of the WebSocket client using the `tungstenite` crate.
#![allow(deprecated)] // TODO(emilk): Remove when we update tungstenite

use std::{
ops::ControlFlow,
Expand Down Expand Up @@ -95,7 +95,7 @@ pub fn ws_receiver_blocking(url: &str, options: Options, on_event: &EventHandler
}

loop {
let control = match socket.read() {
let control = match socket.read_message() {
Ok(incoming_msg) => match incoming_msg {
tungstenite::protocol::Message::Text(text) => {
on_event(WsEvent::Message(WsMessage::Text(text)))
Expand Down Expand Up @@ -155,7 +155,7 @@ pub(crate) fn ws_connect_impl(

/// Connect and call the given event handler on each received event.
///
/// This is a blocking variant of [`crate::ws_connect`], only available on native.
/// This is a blocking variant of [`ws_connect`], only available on native.
///
/// # Errors
/// All errors are returned to the caller, and NOT reported via `on_event`.
Expand All @@ -165,7 +165,6 @@ pub fn ws_connect_blocking(
on_event: &EventHandler,
rx: &Receiver<WsMessage>,
) -> Result<()> {
let delay = options.delay_blocking;
let config = tungstenite::protocol::WebSocketConfig::from(options);
let max_redirects = 3; // tungstenite default
let (mut socket, response) =
Expand Down Expand Up @@ -217,22 +216,22 @@ pub fn ws_connect_blocking(
WsMessage::Pong(data) => tungstenite::protocol::Message::Pong(data),
WsMessage::Unknown(_) => panic!("You cannot send WsMessage::Unknown"),
};
if let Err(err) = socket.write(outgoing_message) {
if let Err(err) = socket.write_message(outgoing_message) {
socket.close(None).ok();
socket.flush().ok();
socket.write_pending().ok();
return Err(format!("send: {err}"));
}
}
Err(TryRecvError::Disconnected) => {
log::debug!("WsSender dropped - closing connection.");
socket.close(None).ok();
socket.flush().ok();
socket.write_pending().ok();
return Ok(());
}
Err(TryRecvError::Empty) => {}
};

let control = match socket.read() {
let control = match socket.read_message() {
Ok(incoming_msg) => {
did_work = true;
match incoming_msg {
Expand Down Expand Up @@ -274,7 +273,7 @@ pub fn ws_connect_blocking(
}

if !did_work {
std::thread::sleep(delay);
std::thread::sleep(std::time::Duration::from_millis(10)); // TODO(emilk): make configurable
}
}
}
1 change: 0 additions & 1 deletion ewebsock/src/tungstenite_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ impl From<crate::Options> for tungstenite::protocol::WebSocketConfig {
fn from(options: crate::Options) -> Self {
let crate::Options {
max_incoming_frame_size,
..
} = options;

Self {
Expand Down

0 comments on commit 93a78df

Please sign in to comment.