It works now part 2

This commit is contained in:
2024-05-21 23:53:40 -06:00
parent a2239cfe7e
commit a362a3e4ff
4 changed files with 484 additions and 15 deletions
+139 -4
View File
@@ -1,17 +1,36 @@
pub mod app;
use std::cell::RefCell;
use std::pin::pin;
use std::rc::Rc;
use std::time::Duration;
use app::STATE;
use asynchronous_codec::Decoder;
use asynchronous_codec::Encoder;
use futures::AsyncRead;
use futures::AsyncWrite;
use futures::Sink;
use futures::SinkExt;
use futures::Stream;
use futures::StreamExt;
use mumble_protocol::control::ControlCodec;
use mumble_protocol::control::ControlPacket;
use mumble_protocol::control::{msgs, ClientControlCodec};
use mumble_protocol::Clientbound;
use mumble_protocol::Serverbound;
use wasm_bindgen::prelude::*;
use web_sys::console;
use web_sys::WebTransport;
use web_sys::WebTransportOptions;
use wasm_bindgen_futures::spawn_local as spawn;
#[wasm_bindgen]
extern "C" {
pub fn alert(s: &str);
}
//pub async fn entrypoint() -> std::result::Result<(), wasm_bindgen::JsValue> {
pub async fn network_entrypoint() {
console::log_1(&"Rust via WASM!".into());
@@ -30,7 +49,8 @@ pub async fn network_entrypoint() {
)
.unwrap();
web_sys::js_sys::Reflect::set(&object, &JsValue::from_str("value"), &hash).unwrap();
//web_sys::js_sys::Reflect::set(&object, &JsValue::from_str("value"), &hash).unwrap();
web_sys::js_sys::Reflect::set(&object, &"value".into(), &hash).unwrap();
let array = web_sys::js_sys::Array::new();
array.push(&object);
@@ -78,11 +98,126 @@ pub async fn network_entrypoint() {
}
};
let wasm_stream_readable = wasm_streams::ReadableStream::from_raw(stream.readable().into());
let wasm_stream_writable = wasm_streams::WritableStream::from_raw(stream.writable().into());
let read_codec = ClientControlCodec::new();
let write_codec = ClientControlCodec::new();
let mut reader = asynchronous_codec::FramedRead::new(wasm_stream_readable.into_async_read(), read_codec);
let mut writer = asynchronous_codec::FramedWrite::new(wasm_stream_writable.into_async_write(), write_codec);
let (send_chan, writer_recv_chan) = async_std::channel::unbounded();
spawn(async move {
while let Ok(msg) = writer_recv_chan.recv().await {
if let Err(e) = writer.send(msg).await {
console::log_1(&e.to_string().into());
break;
}
}
});
// Get version packet
let version = reader.next().await.unwrap().unwrap();
console::log_1(&"Got version packet".into());
console::log_1(&format!("{:#?}", version).into());
// Send version packet
let mut msg = msgs::Version::new();
msg.set_version(0x000010204);
msg.set_release(format!("{} {}", "mumbleweb2", "6.9.0"));
//msg.set_os("Chrome".to_string());
send_chan.send(msg.into()).await.unwrap();
console::log_1(&"Sent version packet".into());
// Send authenticate packet
let mut msg = msgs::Authenticate::new();
msg.set_username("mumbleweb2".to_string());
send_chan.send(msg.into()).await.unwrap();
console::log_1(&"Sent authenticate packet".into());
{
let send_chan = send_chan.clone();
spawn(async move {
loop {
console::log_1(&"Sending ping".into());
if let Err(e) = send_chan.send(msgs::Ping::new().into()).await {
console::log_1(&e.to_string().into());
break;
}
async_std::task::sleep(Duration::from_millis(3000)).await;
}
});
}
loop {
match reader.next().await {
Some(Ok(msg)) => {
console::log_1(&format!("{:#?}", msg).into());
},
None => {
break;
},
Some(Err(e)) => {
console::log_1(&e.to_string().into());
break;
},
}
}
//async fn handle_send(
// mut writer: Rc<RefCell<asynchronous_codec::FramedWrite<impl AsyncRead + AsyncWrite + Unpin, ControlCodec<Serverbound, Clientbound>>>>,
// send_queue: Queue<mumble_protocol::Serverbound>,
//) {
// loop {
// let msg = send_queue.get();
// client.send(msg).await.unwrap();
// }
//}
//async fn handle_ping(
//) {
// //pin!(client);
// loop {
// let ping = msgs::Ping::new();
// client.borrow_mut().send(ping.into()).await.unwrap();
// console::log_1(&"Sent ping packet".into());
//
// async_std::task::sleep(Duration::from_millis(3000)).await;
// }
//}
//let queue_write, queue_read = Queue::new();
//spawn(handle_send(writer, queue_write));
//spawn(handle_ping(queue_read));
//loop {
// let msg = reader.next().await.unwrap().unwrap();
// console::log_1(&format!("{:#?}", msg).into());
//}
// let result: Option<(Version, u32)> = loop {
// match client.next().await {
// None => break None,
// Some(packet) => {
// let packet = packet.unwrap();
// }
// }
// };
//let client = ClientControlCodec::new().framed(stream);
//let client = ClientControlCodec::new().framed(stream);
//let reader = stream.readable();
//let writer = stream.writable();
console::log_1(&"Created bidirectional stream!".into());
console::log_1(&stream.into());
//console::log_1(&stream.into());
//*STATE.status.write() = "Ready!".into();
//return stream.into();
}