diff --git a/CHANGELOG.md b/CHANGELOG.md index badd59de..213b2117 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## Release 0.12.2 +- Reduced *WebSocket* latency. + ## Release 0.12.1 - *WebSocket* now can returns when send correctly a `SendStatus::MaxPacketSizeExceeded` instead of `ResourceNotFound` if the packet size is exceeded. - *UDP* has increases the packet size when send. diff --git a/Cargo.lock b/Cargo.lock index 092ca7e6..498338f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -506,7 +506,7 @@ dependencies = [ [[package]] name = "message-io" -version = "0.12.1" +version = "0.12.2" dependencies = [ "bincode", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 10e44f21..bbc4c7a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "message-io" -version = "0.12.1" +version = "0.12.2" authors = ["lemunozm "] edition = "2018" readme = "README.md" diff --git a/src/adapters/ws.rs b/src/adapters/ws.rs index 98036b6d..f99347e1 100644 --- a/src/adapters/ws.rs +++ b/src/adapters/ws.rs @@ -114,10 +114,24 @@ impl Remote for RemoteResource { match web_socket.read_message() { Ok(message) => match message { Message::Binary(data) => { + // As an optimization. + // Fast check to knwo if there is more data to avoid call + // WebSocker::read_message() again. + let mut should_wait = false; + if let Err(err) = web_socket.get_ref().peek(&mut [0; 0]) { + if err.kind() == ErrorKind::WouldBlock { + should_wait = true; + } + } + // We can not call process_data while the socket is blocked. // The user could lock it again if sends from the callback. drop(state); process_data(&data); + + if should_wait { + break ReadStatus::WaitNextEvent + } } Message::Close(_) => break ReadStatus::Disconnected, _ => continue,