diff --git a/Cargo.lock b/Cargo.lock index f6fa494..cda70f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,7 +62,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" name = "client" version = "0.1.0" dependencies = [ + "js-sys", "wasm-bindgen", + "web-sys", ] [[package]] @@ -330,6 +332,15 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" +[[package]] +name = "js-sys" +version = "0.3.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445dde2150c55e483f3d8416706b97ec8e8237c307e5b7b4b8dd15e6af2a0730" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -531,6 +542,7 @@ version = "0.1.0" dependencies = [ "futures", "hyper", + "lazy_static", "log", "protocol", "rmp-serde", @@ -895,6 +907,16 @@ version = "0.2.84" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d" +[[package]] +name = "web-sys" +version = "0.3.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e33b99f4b23ba3eec1a53ac264e35a755f00e966e0065077d6027c0f575b0b97" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/client/Cargo.toml b/client/Cargo.toml index f46839e..54d3e1a 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -9,4 +9,6 @@ edition = "2021" crate-type = ["cdylib"] [dependencies] -wasm-bindgen = "0.2" \ No newline at end of file +wasm-bindgen = "0.2" +js-sys = "0.3" +web-sys = { version = "0.3", features = ["CanvasRenderingContext2d", "Document", "Element", "HtmlCanvasElement", "Window"]} \ No newline at end of file diff --git a/client/src/lib.rs b/client/src/lib.rs index d6fe5ca..71e5564 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -6,6 +6,7 @@ extern { } #[wasm_bindgen] -pub fn greet(name: &str) { - alert(&format!("Hello, {}!", name)) -} \ No newline at end of file +pub fn send_chat(chat: &str) { + println!("sending chat: {}", chat); +} + diff --git a/server/Cargo.toml b/server/Cargo.toml index 3c9f485..2205eed 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -15,4 +15,5 @@ tungstenite = { version = "0.18", default-features = false } tokio-tungstenite = { version = "0.18" } log = "0.4" simple_logger = "4.1" -protocol = { version = "0.1.0", path = "../protocol" } \ No newline at end of file +protocol = { version = "0.1.0", path = "../protocol" } +lazy_static = "1.4.0" \ No newline at end of file diff --git a/server/src/main.rs b/server/src/main.rs index 7530990..e0b73dc 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -6,10 +6,11 @@ use hyper::service::{make_service_fn, service_fn}; use tokio_tungstenite::WebSocketStream; use tungstenite::{handshake, Error}; use futures::stream::StreamExt; +use lazy_static::lazy_static; use log::{error, info}; use tokio::sync::RwLock; use protocol::State; -use crate::handler::{ClientData, ClientHandler, ClientManager}; +use crate::handler::{ClientHandler, ClientManager}; use crate::wsserver::handle_client; pub mod wsserver; @@ -99,15 +100,18 @@ async fn handle_request(mut request: Request
, remote_addr: SocketAddr, mgr } } } + +lazy_static! { + static ref cmgr: ClientManager = ClientManager { + clients: Arc::new(RwLock::new(Default::default())), + usernames: Arc::new(RwLock::new(Default::default())), + }; +} + #[tokio::main] async fn main() { simple_logger::init_with_env().expect("Unable to start logging service"); - info!("Creating client handler"); - let mut mgr: ClientManager = ClientManager { - clients: Arc::new(RwLock::new(Default::default())), - }; - let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); info!("Listening on {} for HTTP/WebSocket connections", addr); @@ -115,10 +119,11 @@ async fn main() { let make_svc = make_service_fn(|conn: &AddrStream| { let remote_addr = conn.remote_addr(); - let mgr_for_this_thread = mgr.clone(); async move { - Ok::<_, Infallible>(service_fn(move |request: Request| { - handle_request(request, remote_addr, mgr_for_this_thread) + Ok::<_, Infallible>(service_fn({ + move |request: Request| { + handle_request(request, remote_addr, cmgr.clone()) + } })) } }); diff --git a/server/src/timer.rs b/server/src/timer.rs index 73cbb34..301313b 100644 --- a/server/src/timer.rs +++ b/server/src/timer.rs @@ -9,7 +9,7 @@ pub async fn timer_main(mgr: ClientManager) { loop { sleep(Duration::from_millis(5)).await; - for client_thread in mgr.clients.read().await.iter() { + for (addr, client_thread) in mgr.clients.read().await.iter() { match client_thread.tx.send(ClientHandlerMessage::Tick).await { Ok(_) => (), Err(e) => { diff --git a/web/play.html b/web/play.html new file mode 100644 index 0000000..b84c57e --- /dev/null +++ b/web/play.html @@ -0,0 +1,31 @@ + + + +hello: blsdkjf
+