From 4365b12e27955e51bfeb114caed69f8f52f26bd4 Mon Sep 17 00:00:00 2001 From: c0repwn3r Date: Sun, 9 Apr 2023 23:34:01 -0400 Subject: [PATCH] ultra basic chat ui --- client/Cargo.toml | 14 ++++++++++++-- client/src/lib.rs | 11 ++++++++++- justfile | 3 +++ web/index.html | 2 +- web/play.html | 2 +- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/client/Cargo.toml b/client/Cargo.toml index e5b99e29170fa8f3118da023d927f826d5a33696..f1c8dd4f20250492bb87cdeff0d569c4a8ccf884 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -11,7 +11,6 @@ crate-type = ["cdylib"] [dependencies] wasm-bindgen = "0.2" js-sys = "0.3" -web-sys = { version = "0.3", features = ["CanvasRenderingContext2d", "Document", "Element", "HtmlCanvasElement", "Window"]} console_log = { version = "1", features = ["color"] } log = "0.4" futures = { version = "0.3", default-features = false } @@ -21,4 +20,15 @@ protocol = { version = "0.1.0", path = "../protocol" } rmp-serde = "1.1" ws_stream_wasm = "0.7" serde = { version = "1", features = ["derive"] } -lazy_static = "1.4" \ No newline at end of file +lazy_static = "1.4" + + +[dependencies.web-sys] +version = "0.3.4" +features = [ + 'Document', + 'Element', + 'HtmlElement', + 'Node', + 'Window', +] \ No newline at end of file diff --git a/client/src/lib.rs b/client/src/lib.rs index ec59ab2bcd45470a04e5654f9833b26d6a4dac47..48427e66cb0c6f63ecf55a8bf00dcd8a1154fd56 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -13,6 +13,7 @@ use lazy_static::lazy_static; use std::sync::Arc; use std::sync::RwLock; use futures::FutureExt; +use web_sys::Window; #[macro_use] pub mod macros; @@ -133,7 +134,15 @@ pub async fn update_socket() -> Result<(), JsError> { } MessageS2C::Chat { from, message } => { info!("[CHAT] {}: {}", from, message); - // TODO: Handle + + let window: Window = web_sys::window().expect("no global `window` exists"); + let document = window.document().expect("should have a document on window"); + let chatbox = document.get_element_by_id("chats").expect("chatbox does not exist"); + + let new_elem = document.create_element("p").expect("could not create element"); + new_elem.set_inner_html(&format!("{}: {}", from, message)); + + chatbox.append_child(&new_elem).unwrap(); }, _ => { warn!("server sent unexpected packet {:?}, ignoring", msg); diff --git a/justfile b/justfile index eaee190f078741334d7dd9ed32c4bddb2a3df0ef..4f49d454f7e088d50a197a8a4cefd327c86d0ac2 100644 --- a/justfile +++ b/justfile @@ -1,6 +1,9 @@ run_server: build_client_bundle cargo run --bin server +run_http: build_client_bundle + cd web && python3 -m http.server + build_client_bundle: rm -rf web/dist RUST_BACKTRACE=1 wasm-pack build --target web client diff --git a/web/index.html b/web/index.html index dc51831e7a7f5165d79da2acf72ac78c2001184b..1732ce5116f266bf1e17ea81214a20fa128f0974 100644 --- a/web/index.html +++ b/web/index.html @@ -6,7 +6,7 @@ -
+
diff --git a/web/play.html b/web/play.html index 9a350077edd612526f9ab8dcaedccae5e0ad5d6c..52c49aecbb1444d0193938d796272fb642c9c04b 100644 --- a/web/play.html +++ b/web/play.html @@ -11,7 +11,7 @@
-

hello: blsdkjf

+