Skip to content

Commit

Permalink
Initial barebones for browser wasm support
Browse files Browse the repository at this point in the history
  • Loading branch information
simlay committed Jul 29, 2024
1 parent 8325825 commit 80526fb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ rustls-tls-webpki-roots = ["__rustls-tls", "webpki-roots"]
__rustls-tls = ["rustls", "rustls-pki-types", "tokio-rustls", "stream", "tungstenite/__rustls-tls", "handshake"]
stream = []
url = ["tungstenite/url"]
js-connect = ["stream", "handshake", "ws_stream_wasm", "async_io_stream", "getrandom/js" ]

[dependencies]
log = "0.4.17"
futures-util = { version = "0.3.28", default-features = false, features = ["sink", "std"] }
tokio = { version = "1.0.0", default-features = false, features = ["io-util"] }
tokio = { version = "1.39.0", default-features = false }
ws_stream_wasm = { version = "0.7.4", optional = true }
async_io_stream = { version = "0.3.3", features = ["tokio_io"], optional = true }
getrandom = { version = "0.2", optional = true }

[dependencies.tungstenite]
version = "0.23.0"
Expand Down Expand Up @@ -68,7 +72,7 @@ default-features = false
optional = true
version = "0.26.0"

[dev-dependencies]
[target.'cfg(not(target_family = "wasm"))'.dev-dependencies]
futures-channel = "0.3.28"
hyper = { version = "1.0", default-features = false, features = ["http1", "server"] }
hyper-util = { version = "0.1", features = ["tokio"] }
Expand Down
42 changes: 42 additions & 0 deletions src/connect_wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::{stream::MaybeTlsStream, Connector, WebSocketStream};

use tungstenite::{
error::{Error, UrlError},
handshake::client::{Request, Response},
protocol::WebSocketConfig,
//client::IntoClientRequest,
};
//use web_sys::WebSocket;
use ws_stream_wasm::WsStreamIo;
use async_io_stream::IoStream;



pub async fn connect(
request: Request,
config: Option<WebSocketConfig>,
disable_nagle: bool,
//connector: Option<Connector>,
) -> Result<(WebSocketStream<MaybeTlsStream<IoStream<WsStreamIo, Vec<u8>>>>, Response), Error> {
//let domain = domain(&request)?;
let domain = request.uri().host().unwrap();
let port = request
.uri()
.port_u16()
.or_else(|| match request.uri().scheme_str() {
Some("wss") => Some(443),
Some("ws") => Some(80),
_ => None,
})
.ok_or(Error::Url(UrlError::UnsupportedUrlScheme))?;

let addr = format!("{domain}:{port}");
//let socket = TcpStream::connect(addr).await.map_err(Error::Io)?;

if disable_nagle {
//socket.set_nodelay(true)?;
}
todo!();

//crate::tls::client_async_tls_with_config(request, socket, config, connector).await
}
18 changes: 13 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@
//! Each WebSocket stream implements the required `Stream` and `Sink` traits,
//! so the socket is just a stream of messages coming in and going out.
#![deny(missing_docs, unused_must_use, unused_mut, unused_imports, unused_import_braces)]
//#![deny(missing_docs, unused_must_use, unused_mut, unused_imports, unused_import_braces)]

pub use tungstenite;

mod compat;
#[cfg(feature = "connect")]
#[cfg(all(feature = "connect", not(target_family = "wasm")))]
mod connect;

#[cfg(all(feature = "js-connect", target_family = "wasm"))]
mod connect_wasm;

#[cfg(feature = "handshake")]
mod handshake;
#[cfg(feature = "stream")]
mod stream;
#[cfg(any(feature = "native-tls", feature = "__rustls-tls", feature = "connect"))]
#[cfg(any(feature = "native-tls", feature = "__rustls-tls", feature = "connect", feature = "js-connect"))]
mod tls;

use std::io::{Read, Write};
Expand Down Expand Up @@ -49,14 +54,17 @@ use tungstenite::{
protocol::{Message, Role, WebSocket, WebSocketConfig},
};

#[cfg(any(feature = "native-tls", feature = "__rustls-tls", feature = "connect"))]
#[cfg(any(feature = "native-tls", feature = "__rustls-tls", feature = "connect", feature = "js-connect"))]
pub use tls::Connector;
#[cfg(any(feature = "native-tls", feature = "__rustls-tls"))]
pub use tls::{client_async_tls, client_async_tls_with_config};

#[cfg(feature = "connect")]
#[cfg(all(feature = "connect", not(target_family = "wasm")))]
pub use connect::{connect_async, connect_async_with_config};

#[cfg(all(feature = "js-connect", target_family = "wasm"))]
pub use connect_wasm::connect;

#[cfg(all(any(feature = "native-tls", feature = "__rustls-tls"), feature = "connect"))]
pub use connect::connect_async_tls_with_config;

Expand Down

0 comments on commit 80526fb

Please sign in to comment.