Initial commit...it works?

This commit is contained in:
2024-05-21 16:25:32 -06:00
commit a04238dbcd
7 changed files with 369 additions and 0 deletions
+84
View File
@@ -0,0 +1,84 @@
use wasm_bindgen::prelude::*;
use web_sys::console;
use web_sys::WebTransport;
use web_sys::WebTransportOptions;
use web_sys::js_sys::Promise;
#[wasm_bindgen]
extern "C" {
pub fn alert(s: &str);
}
#[wasm_bindgen]
//pub async fn entrypoint() -> std::result::Result<(), wasm_bindgen::JsValue> {
pub async fn entrypoint() -> JsValue {
console::log_1(&"Rust via WASM!".into());
let server_hash = vec![
14, 162, 111, 176, 34, 113, 218, 69, 177, 18, 13, 180, 232, 204, 49, 65, 161, 195, 36, 238,
23, 95, 174, 190, 24, 216, 105, 89, 236, 147, 206, 139,
];
let hash = web_sys::js_sys::Uint8Array::from(server_hash.as_slice());
let object = web_sys::js_sys::Object::new();
web_sys::js_sys::Reflect::set(
&object,
&JsValue::from_str("algorithm"),
&JsValue::from_str("sha-256"),
)
.unwrap();
web_sys::js_sys::Reflect::set(&object, &JsValue::from_str("value"), &hash).unwrap();
let array = web_sys::js_sys::Array::new();
array.push(&object);
console::log_1(&object.clone().into());
console::log_1(&"Created option object!".into());
let mut options = WebTransportOptions::new();
options.server_certificate_hashes(&array);
console::log_1(&"Created WebTransportOptions!".into());
let transport = match WebTransport::new_with_options(
"https://localhost:4433/?hostname=ohea.xyz&port=64738&username=test",
&options,
) {
Ok(x) => x,
Err(e) => {
console::log_1(&e.into());
panic!();
}
};
console::log_1(&"Created WebTransport connection object.".into());
console::log_1(&transport.clone().into());
if let Err(e) = wasm_bindgen_futures::JsFuture::from(transport.ready()).await {
console::log_1(&e.into());
panic!();
}
console::log_1(&"Transport is ready.".into());
let stream: web_sys::WebTransportBidirectionalStream =
match wasm_bindgen_futures::JsFuture::from(transport.create_bidirectional_stream()).await {
Ok(x) => {
x.into()
}
Err(e) => {
console::log_1(&e.into());
panic!();
}
};
//let reader = stream.readable();
//let writer = stream.writable();
console::log_1(&"Created bidirectional stream!".into());
return stream.into();
}