Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: New WASM implementation #19

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ rustdoc-args = ["--cfg", "docsrs"]
json = ["dep:serde", "dep:serde_json"]

[dependencies]
futures-util = { version = "0.3", default-features = false, features = ["sink"] }
futures-util = { version = "0.3", default-features = false, features = ["sink", "std", "async-await-macro"] }
futures-channel = { version = "0.3", default-features = false, features = ["sink", "std"] }
reqwest = { version = "0.12", default-features = false }
thiserror = "1"
tracing = "0.1"
Expand All @@ -37,14 +38,12 @@ tungstenite = { version = "0.23", default-features = false, features = ["handsha

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "0.3", features = ["WebSocket", "CloseEvent", "ErrorEvent", "Event", "MessageEvent", "BinaryType"] }
tokio = { version = "1", default-features = false, features = ["sync", "macros"] }
wasm-bindgen-futures = "0.4"

[dev-dependencies]
tokio = { version = "1", features = ["macros", "rt"] }
reqwest = { version = "0.12", features = ["default-tls"] }
serde = { version = "1.0", features = ["derive"] }
futures-util = { version = "0.3", default-features = false, features = ["sink", "alloc"] }

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3"
wasm-bindgen-futures = "0.4"
2 changes: 2 additions & 0 deletions examples/wasm/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub fn App() -> impl IntoView {

spawn_local(async move {
let websocket = reqwest_websocket::websocket("https://echo.websocket.org/").await.unwrap();
tracing::info!("WebSocket connected");

let (mut sender, mut receiver) = websocket.split();

futures::join!(
Expand Down
32 changes: 26 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub enum Error {
#[cfg(target_arch = "wasm32")]
#[cfg_attr(docsrs, doc(cfg(target_arch = "wasm32")))]
#[error("web_sys error")]
WebSys(#[from] wasm::WebSysError),
WebSys(#[from] wasm::Error),

/// Error during serialization/deserialization.
#[error("serde_json error")]
Expand Down Expand Up @@ -160,7 +160,7 @@ impl UpgradedRequestBuilder {
let inner = native::send_request(self.inner).await?;

#[cfg(target_arch = "wasm32")]
let inner = wasm::WebSysWebSocketStream::new(self.inner.build()?, &self.protocols).await?;
let inner = wasm::WebSocket::new(self.inner.build()?, &self.protocols).await?;

Ok(UpgradeResponse {
inner,
Expand All @@ -178,7 +178,7 @@ pub struct UpgradeResponse {
inner: native::WebSocketResponse,

#[cfg(target_arch = "wasm32")]
inner: wasm::WebSysWebSocketStream,
inner: wasm::WebSocket,

#[allow(dead_code)]
protocols: Vec<String>,
Expand All @@ -202,7 +202,7 @@ impl UpgradeResponse {

#[cfg(target_arch = "wasm32")]
let (inner, protocol) = {
let protocol = self.inner.protocol();
let protocol = self.inner.protocol().to_owned();
(self.inner, Some(protocol))
};

Expand All @@ -225,7 +225,7 @@ pub struct WebSocket {
inner: native::WebSocketStream,

#[cfg(target_arch = "wasm32")]
inner: wasm::WebSysWebSocketStream,
inner: wasm::WebSocket,

protocol: Option<String>,
}
Expand Down Expand Up @@ -256,7 +256,15 @@ impl WebSocket {
}

#[cfg(target_arch = "wasm32")]
self.inner.close(code.into(), reason.unwrap_or_default())?;
{
let mut inner = self.inner;
inner
.send(Message::Close {
code,
reason: reason.unwrap_or_default().to_owned(),
})
.await?;
}

Ok(())
}
Expand Down Expand Up @@ -319,6 +327,18 @@ pub mod tests {

use super::{websocket, CloseCode, Message, RequestBuilderExt, WebSocket};

macro_rules! assert_send {
($ty:ty) => {
const _: () = {
struct Assert<T: Send>(std::marker::PhantomData<T>);
Assert::<$ty>(std::marker::PhantomData);
};
};
}

// unfortunately hyper IO is not sync
assert_send!(WebSocket);

async fn test_websocket(mut websocket: WebSocket) {
let text = "Hello, World!";
websocket
Expand Down
Loading