beginnings of ws connect

This commit is contained in:
CoolElectronics 2024-01-13 23:38:29 -05:00
parent 902442b7ba
commit c401b4b28b
No known key found for this signature in database
GPG key ID: F63593D168636C50
3 changed files with 69 additions and 7 deletions

20
Cargo.lock generated
View file

@ -246,6 +246,19 @@ version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5"
[[package]]
name = "fastwebsockets"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f63dd7b57f9b33b1741fa631c9522eb35d43e96dcca4a6a91d5e4ca7c93acdc1"
dependencies = [
"rand",
"simdutf8",
"thiserror",
"tokio",
"utf-8",
]
[[package]] [[package]]
name = "flate2" name = "flate2"
version = "1.0.28" version = "1.0.28"
@ -966,6 +979,12 @@ dependencies = [
"digest", "digest",
] ]
[[package]]
name = "simdutf8"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a"
[[package]] [[package]]
name = "slab" name = "slab"
version = "0.4.9" version = "0.4.9"
@ -1527,6 +1546,7 @@ dependencies = [
"bytes", "bytes",
"console_error_panic_hook", "console_error_panic_hook",
"either", "either",
"fastwebsockets",
"futures-util", "futures-util",
"getrandom", "getrandom",
"http 1.0.0", "http 1.0.0",

View file

@ -30,6 +30,7 @@ wasm-streams = "0.4.0"
either = "1.9.0" either = "1.9.0"
tokio-util = { version = "0.7.10", features = ["io"] } tokio-util = { version = "0.7.10", features = ["io"] }
async-compression = { version = "0.4.5", features = ["tokio", "gzip", "brotli"] } async-compression = { version = "0.4.5", features = ["tokio", "gzip", "brotli"] }
fastwebsockets = { version = "0.6.0", features=[]}
[dependencies.getrandom] [dependencies.getrandom]
features = ["js"] features = ["js"]

View file

@ -4,6 +4,8 @@ mod utils;
mod tokioio; mod tokioio;
mod wrappers; mod wrappers;
use fastwebsockets::{Frame, OpCode, Payload, Role, WebSocket};
use tokio::io::AsyncWriteExt;
use tokioio::TokioIo; use tokioio::TokioIo;
use utils::{ReplaceErr, UriExt}; use utils::{ReplaceErr, UriExt};
use wrappers::{IncomingBody, WsStreamWrapper}; use wrappers::{IncomingBody, WsStreamWrapper};
@ -14,9 +16,13 @@ use async_compression::tokio::bufread as async_comp;
use bytes::Bytes; use bytes::Bytes;
use futures_util::StreamExt; use futures_util::StreamExt;
use http::{uri, HeaderName, HeaderValue, Request, Response}; use http::{uri, HeaderName, HeaderValue, Request, Response};
use hyper::{body::Incoming, client::conn::http1::Builder, Uri}; use hyper::{
use js_sys::{Array, Object, Reflect, Uint8Array}; body::Incoming,
use penguin_mux_wasm::{Multiplexor, MuxStream, Role}; client::conn::http1::{handshake, Builder},
Uri,
};
use js_sys::{Array, Function, Object, Reflect, Uint8Array};
use penguin_mux_wasm::{Multiplexor, MuxStream};
use tokio_rustls::{client::TlsStream, rustls, rustls::RootCertStore, TlsConnector}; use tokio_rustls::{client::TlsStream, rustls, rustls::RootCertStore, TlsConnector};
use tokio_util::{ use tokio_util::{
either::Either, either::Either,
@ -140,7 +146,7 @@ impl WsTcp {
.await .await
.replace_err("Failed to connect to websocket")?; .replace_err("Failed to connect to websocket")?;
debug!("connected!"); debug!("connected!");
let mux = Multiplexor::new(ws, Role::Client, None, None); let mux = Multiplexor::new(ws, penguin_mux_wasm::Role::Client, None, None);
let mut certstore = RootCertStore::empty(); let mut certstore = RootCertStore::empty();
certstore.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); certstore.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
@ -186,6 +192,41 @@ impl WsTcp {
} }
} }
#[wasm_bindgen]
pub async fn connect_ws(
&self,
url: String,
protocols: Vec<String>,
onopen: Function,
onclose: Function,
onmessage: Function,
onerror: Function,
host: String,
) -> Result<JsValue, JsError> {
onopen.call0(&Object::default());
let uri = url.parse::<uri::Uri>().replace_err("Failed to parse URL")?;
let mut io = self.get_http_io(&uri).await?;
let mut a = WebSocket::after_handshake(io, fastwebsockets::Role::Client);
a.set_writev(false);
a.set_auto_close(true);
a.set_auto_pong(true);
a.write_frame(Frame::new(
true,
OpCode::Text,
None,
Payload::Owned(b"aasdfdfhsdfhkadfhsdfkhjasfkajdfhaksjhfkadhfkashdfkhsd".to_vec()),
))
.await;
// .await
// .replace_err("Failed to connect to host")?;
let closure = Closure::<dyn FnMut(JsValue)>::new(move |data: JsValue| {
log!("WaeASDASd");
});
Ok(closure.into_js_value())
}
async fn send_req( async fn send_req(
&self, &self,
req: http::Request<HttpBody>, req: http::Request<HttpBody>,
@ -375,14 +416,14 @@ impl WsTcp {
if jv.is_array() { if jv.is_array() {
let arr = Array::from(&jv); let arr = Array::from(&jv);
arr.push(&jval!(v.to_str().unwrap().to_string())); arr.push(&jval!(v.to_str().unwrap().to_string()));
let _=Reflect::set(&raw_headers, &jval!(k.to_string()), &arr); let _ = Reflect::set(&raw_headers, &jval!(k.to_string()), &arr);
} else if !jv.is_falsy() { } else if !jv.is_falsy() {
let arr = Array::new(); let arr = Array::new();
arr.push(&jv); arr.push(&jv);
arr.push(&jval!(v.to_str().unwrap().to_string())); arr.push(&jval!(v.to_str().unwrap().to_string()));
let _=Reflect::set(&raw_headers, &jval!(k.to_string()), &arr); let _ = Reflect::set(&raw_headers, &jval!(k.to_string()), &arr);
} else { } else {
let _=Reflect::set( let _ = Reflect::set(
&raw_headers, &raw_headers,
&jval!(k.to_string()), &jval!(k.to_string()),
&jval!(v.to_str().unwrap().to_string()), &jval!(v.to_str().unwrap().to_string()),