Skip to content

Commit

Permalink
bump tungstenite (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-Just-Nans authored Jun 5, 2024
1 parent a865751 commit 1a9e78a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 25 deletions.
16 changes: 7 additions & 9 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion echo_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ publish = false


[dependencies]
tungstenite = { version = ">=0.17, <=0.21" }
tungstenite = { version = "0.23" }
12 changes: 7 additions & 5 deletions echo_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![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 @@ -11,14 +10,17 @@ fn main() {
spawn(move || {
let mut websocket = tungstenite::accept(stream.unwrap()).unwrap();
eprintln!("New client connected");
while let Ok(msg) = websocket.read_message() {
while let Ok(msg) = websocket.read() {
// We do not want to send back ping/pong messages.
if msg.is_binary() || msg.is_text() {
if websocket.write_message(msg).is_ok() {
eprintln!("Responded.");
} else {
if let Err(err) = websocket.send(msg) {
eprintln!("Error sending message: {err}");
break;
} else {
eprintln!("Responded.");
}
} else {
eprintln!("Message received not text or binary.");
}
}
eprintln!("Client left.");
Expand Down
4 changes: 2 additions & 2 deletions ewebsock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ log = "0.4"

# native:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tungstenite = { version = ">=0.17, <=0.21" }
tungstenite = { version = "0.23" }

# Optional dependencies for feature "tokio":
async-stream = { version = "0.3", optional = true }
Expand All @@ -57,7 +57,7 @@ futures-util = { version = "0.3", optional = true, default-features = false, fea
"std",
] }
tokio = { version = "1.16", features = ["rt", "sync"], optional = true }
tokio-tungstenite = { version = ">=0.17, <=0.21", optional = true }
tokio-tungstenite = { version = "0.23", optional = true }

# web:
[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
4 changes: 4 additions & 0 deletions ewebsock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,16 @@ 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: 9 additions & 8 deletions ewebsock/src/native_tungstenite.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(deprecated)] // TODO(emilk): Remove when we update tungstenite
//! Native implementation of the WebSocket client using the `tungstenite` crate.
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_message() {
let control = match socket.read() {
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 [`ws_connect`], only available on native.
/// This is a blocking variant of [`crate::ws_connect`], only available on native.
///
/// # Errors
/// All errors are returned to the caller, and NOT reported via `on_event`.
Expand All @@ -165,6 +165,7 @@ 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 @@ -216,22 +217,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_message(outgoing_message) {
if let Err(err) = socket.write(outgoing_message) {
socket.close(None).ok();
socket.write_pending().ok();
socket.flush().ok();
return Err(format!("send: {err}"));
}
}
Err(TryRecvError::Disconnected) => {
log::debug!("WsSender dropped - closing connection.");
socket.close(None).ok();
socket.write_pending().ok();
socket.flush().ok();
return Ok(());
}
Err(TryRecvError::Empty) => {}
};

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

if !did_work {
std::thread::sleep(std::time::Duration::from_millis(10)); // TODO(emilk): make configurable
std::thread::sleep(delay);
}
}
}
1 change: 1 addition & 0 deletions ewebsock/src/tungstenite_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 1a9e78a

Please sign in to comment.