aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2021-12-27 22:18:46 +0100
committerIlion Beyst <ilion.beyst@gmail.com>2021-12-27 22:18:46 +0100
commit5ca8dd4c842ee681ce81a6a7bbd5005cd5b98d3c (patch)
tree1273b264a806a7e1867a6a235998a9f4daaef0ed
parent109e06ecad14dc73a8910c859f85a46ae6b61c59 (diff)
downloadplanetwars.dev-5ca8dd4c842ee681ce81a6a7bbd5005cd5b98d3c.tar.xz
planetwars.dev-5ca8dd4c842ee681ce81a6a7bbd5005cd5b98d3c.zip
add websocket stub
-rw-r--r--planetwars-cli/Cargo.toml2
-rw-r--r--planetwars-cli/src/web/mod.rs23
-rw-r--r--web/pw-frontend/vite.config.js1
3 files changed, 24 insertions, 2 deletions
diff --git a/planetwars-cli/Cargo.toml b/planetwars-cli/Cargo.toml
index 977ce18..25b6e74 100644
--- a/planetwars-cli/Cargo.toml
+++ b/planetwars-cli/Cargo.toml
@@ -20,5 +20,5 @@ clap = { version = "3.0.0-rc.8", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] }
rust-embed = "6.3.0"
-axum = "0.4"
+axum = { version = "0.4", features = ["ws"] }
mime_guess = "2" \ No newline at end of file
diff --git a/planetwars-cli/src/web/mod.rs b/planetwars-cli/src/web/mod.rs
index 66d4218..8394897 100644
--- a/planetwars-cli/src/web/mod.rs
+++ b/planetwars-cli/src/web/mod.rs
@@ -1,6 +1,6 @@
use axum::{
body::{boxed, Full},
- extract::{Extension, Path},
+ extract::{ws::WebSocket, Extension, Path, WebSocketUpgrade},
handler::Handler,
http::{header, StatusCode, Uri},
response::{IntoResponse, Response},
@@ -36,6 +36,7 @@ pub async fn run(workspace_root: PathBuf) {
// build our application with a route
let app = Router::new()
.route("/", get(index_handler))
+ .route("/ws", get(ws_handler))
.route("/api/matches", get(list_matches))
.route("/api/matches/:match_id", get(get_match))
.fallback(static_handler.into_service())
@@ -50,6 +51,26 @@ pub async fn run(workspace_root: PathBuf) {
.unwrap();
}
+async fn ws_handler(ws: WebSocketUpgrade) -> impl IntoResponse {
+ ws.on_upgrade(handle_socket)
+}
+
+async fn handle_socket(mut socket: WebSocket) {
+ while let Some(msg) = socket.recv().await {
+ let msg = if let Ok(msg) = msg {
+ msg
+ } else {
+ // client disconnected
+ return;
+ };
+
+ if socket.send(msg).await.is_err() {
+ // client disconnected
+ return;
+ }
+ }
+}
+
#[derive(Serialize, Deserialize)]
struct MatchData {
name: String,
diff --git a/web/pw-frontend/vite.config.js b/web/pw-frontend/vite.config.js
index a298f2c..a7fcc74 100644
--- a/web/pw-frontend/vite.config.js
+++ b/web/pw-frontend/vite.config.js
@@ -20,6 +20,7 @@ export default defineConfig({
server: {
proxy: {
"/api/": "http://localhost:5000",
+ "/ws": "ws://localhost:5000/ws",
},
},
})