M client/Cargo.toml => client/Cargo.toml +12 -2
@@ 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
M client/src/lib.rs => client/src/lib.rs +10 -1
@@ 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!("<b>{}</b>: {}", from, message));
+
+ chatbox.append_child(&new_elem).unwrap();
},
_ => {
warn!("server sent unexpected packet {:?}, ignoring", msg);
M justfile => justfile +3 -0
@@ 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
M web/index.html => web/index.html +1 -1
@@ 6,7 6,7 @@
</head>
<body>
- <form target="/play.html" method="GET">
+ <form action="/play.html" method="GET">
<label for="server">Gateway server</label>
<input type="text" name="server" id="server" value="ws://localhost:3000/ws" required />
<br>
M web/play.html => web/play.html +1 -1
@@ 11,7 11,7 @@
<body>
<div class="chatbox">
<div id="chats">
- <p>hello: blsdkjf</p>
+
</div>
<input id="chat-value" type="text" placeholder="chat text goes here" />
<button id="chat-submit">submit</button>