hell pt2
This commit is contained in:
parent
8d149bcc6a
commit
01d8dceedc
|
@ -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"
|
||||
|
|
|
@ -10,3 +10,5 @@ crate-type = ["cdylib"]
|
|||
|
||||
[dependencies]
|
||||
wasm-bindgen = "0.2"
|
||||
js-sys = "0.3"
|
||||
web-sys = { version = "0.3", features = ["CanvasRenderingContext2d", "Document", "Element", "HtmlCanvasElement", "Window"]}
|
|
@ -6,6 +6,7 @@ extern {
|
|||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn greet(name: &str) {
|
||||
alert(&format!("Hello, {}!", name))
|
||||
pub fn send_chat(chat: &str) {
|
||||
println!("sending chat: {}", chat);
|
||||
}
|
||||
|
||||
|
|
|
@ -16,3 +16,4 @@ tokio-tungstenite = { version = "0.18" }
|
|||
log = "0.4"
|
||||
simple_logger = "4.1"
|
||||
protocol = { version = "0.1.0", path = "../protocol" }
|
||||
lazy_static = "1.4.0"
|
|
@ -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<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]
|
||||
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<Body>| {
|
||||
handle_request(request, remote_addr, mgr_for_this_thread)
|
||||
Ok::<_, Infallible>(service_fn({
|
||||
move |request: Request<Body>| {
|
||||
handle_request(request, remote_addr, cmgr.clone())
|
||||
}
|
||||
}))
|
||||
}
|
||||
});
|
||||
|
|
|
@ -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) => {
|
||||
|
|
|
@ -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>
|
Loading…
Reference in New Issue