This commit is contained in:
core 2023-04-08 21:35:25 -04:00
parent 8d149bcc6a
commit 01d8dceedc
Signed by: core
GPG Key ID: FDBF740DADDCEECF
8 changed files with 77 additions and 15 deletions

22
Cargo.lock generated
View File

@ -62,7 +62,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
name = "client" name = "client"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"js-sys",
"wasm-bindgen", "wasm-bindgen",
"web-sys",
] ]
[[package]] [[package]]
@ -330,6 +332,15 @@ version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" 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]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@ -531,6 +542,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"futures", "futures",
"hyper", "hyper",
"lazy_static",
"log", "log",
"protocol", "protocol",
"rmp-serde", "rmp-serde",
@ -895,6 +907,16 @@ version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d" 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]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

View File

@ -9,4 +9,6 @@ edition = "2021"
crate-type = ["cdylib"] crate-type = ["cdylib"]
[dependencies] [dependencies]
wasm-bindgen = "0.2" wasm-bindgen = "0.2"
js-sys = "0.3"
web-sys = { version = "0.3", features = ["CanvasRenderingContext2d", "Document", "Element", "HtmlCanvasElement", "Window"]}

View File

@ -6,6 +6,7 @@ extern {
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn greet(name: &str) { pub fn send_chat(chat: &str) {
alert(&format!("Hello, {}!", name)) println!("sending chat: {}", chat);
} }

View File

@ -15,4 +15,5 @@ tungstenite = { version = "0.18", default-features = false }
tokio-tungstenite = { version = "0.18" } tokio-tungstenite = { version = "0.18" }
log = "0.4" log = "0.4"
simple_logger = "4.1" simple_logger = "4.1"
protocol = { version = "0.1.0", path = "../protocol" } protocol = { version = "0.1.0", path = "../protocol" }
lazy_static = "1.4.0"

View File

@ -6,10 +6,11 @@ use hyper::service::{make_service_fn, service_fn};
use tokio_tungstenite::WebSocketStream; use tokio_tungstenite::WebSocketStream;
use tungstenite::{handshake, Error}; use tungstenite::{handshake, Error};
use futures::stream::StreamExt; use futures::stream::StreamExt;
use lazy_static::lazy_static;
use log::{error, info}; use log::{error, info};
use tokio::sync::RwLock; use tokio::sync::RwLock;
use protocol::State; use protocol::State;
use crate::handler::{ClientData, ClientHandler, ClientManager}; use crate::handler::{ClientHandler, ClientManager};
use crate::wsserver::handle_client; use crate::wsserver::handle_client;
pub mod wsserver; pub mod wsserver;
@ -99,15 +100,18 @@ async fn handle_request(mut request: Request<Body>, 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] #[tokio::main]
async fn main() { async fn main() {
simple_logger::init_with_env().expect("Unable to start logging service"); 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)); let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
info!("Listening on {} for HTTP/WebSocket connections", addr); info!("Listening on {} for HTTP/WebSocket connections", addr);
@ -115,10 +119,11 @@ async fn main() {
let make_svc = make_service_fn(|conn: &AddrStream| { let make_svc = make_service_fn(|conn: &AddrStream| {
let remote_addr = conn.remote_addr(); let remote_addr = conn.remote_addr();
let mgr_for_this_thread = mgr.clone();
async move { async move {
Ok::<_, Infallible>(service_fn(move |request: Request<Body>| { Ok::<_, Infallible>(service_fn({
handle_request(request, remote_addr, mgr_for_this_thread) move |request: Request<Body>| {
handle_request(request, remote_addr, cmgr.clone())
}
})) }))
} }
}); });

View File

@ -9,7 +9,7 @@ pub async fn timer_main(mgr: ClientManager) {
loop { loop {
sleep(Duration::from_millis(5)).await; 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 { match client_thread.tx.send(ClientHandlerMessage::Tick).await {
Ok(_) => (), Ok(_) => (),
Err(e) => { Err(e) => {

31
web/play.html Normal file
View File

@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>StarKingdoms.TK</title>
<link rel="stylesheet" href="/static/css/stylemain.css"></link>
<link rel="favicon" href="/static/img/favicon.ico"></link>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
</head>
<body>
<div class="chatbox">
<div id="chats">
<p>hello: blsdkjf</p>
</div>
<input type="text" placeholder="chat text goes here" />
<button id="chat-submit">submit</button>
</div>
<script type="module">
// If you're getting build errors here | you need to run `just build_client_bundle` first, to compile client code
// v
import init, { send_chat } from "./dist/client.js";
init().then(() => {
document.getElementById("chat-submit").addEventListener("click", e => {
send_chat(e.target.value);
})
})
</script>
</body>
</html>

View File