From bfbbfe854bb5b048edea8cab551bd914b963d798 Mon Sep 17 00:00:00 2001 From: Thilo Behnke Date: Wed, 1 Jun 2022 21:11:12 +0200 Subject: [PATCH] network session button --- server/src/http.rs | 8 ++++++-- www/index.html | 6 +++++- www/index.js | 48 +++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/server/src/http.rs b/server/src/http.rs index d5d307f..96534b5 100644 --- a/server/src/http.rs +++ b/server/src/http.rs @@ -146,7 +146,7 @@ async fn handle_session_create(session_manager: &Arc>, mut req: Request, addr: SocketAddr) -> Result, Infallible> { @@ -215,7 +215,11 @@ async fn handle_event_read(session_manager: &Arc>, pub fn build_success_res(value: &str) -> Result, Infallible> { let json = format!("{{\"data\": {}}}", value); - return Ok(Response::new(Body::from(json))); + let mut res = Response::new(Body::from(json)); + let headers = res.headers_mut(); + headers.insert("Content-Type", "application/json".parse().unwrap()); + headers.insert("Access-Control-Allow-Origin", "http://localhost:8080".parse().unwrap()); + Ok(res) } pub fn build_error_res(error: &str, status: StatusCode) -> Result, Infallible> { diff --git a/www/index.html b/www/index.html index 81c5df8..c3374ef 100644 --- a/www/index.html +++ b/www/index.html @@ -20,7 +20,11 @@ -
+ +
+ diff --git a/www/index.js b/www/index.js index 6f55e3e..f0a27b0 100644 --- a/www/index.js +++ b/www/index.js @@ -1,10 +1,8 @@ -import * as wasm from "wasm-app"; import { FieldWrapper, GameObject } from "wasm-app"; -import { memory } from "wasm-app/rust_wasm_bg"; const GRID_COLOR = "#CCCCCC"; -const field = FieldWrapper.new(); +let field = FieldWrapper.new(); const width = field.width(); const height = field.height(); @@ -15,11 +13,20 @@ canvas.width = width const ctx = canvas.getContext('2d'); let paused = false; +let resetRequested = false; let debug = false; let keysDown = new Set(); let actions = []; +let networkSession = null; +let websocket = null; + const renderLoop = () => { + if (resetRequested) { + resetRequested = false; + reset(); + return; + } actions = getInputActions(); if (paused) { requestAnimationFrame(renderLoop); @@ -39,10 +46,45 @@ const render = () => { drawObjects(); } +const reset = () => { + paused = false; + keysDown = new Set(); + actions = []; + field = FieldWrapper.new(); + + networkSession = null; + if (websocket) { + websocket.close(); + } + websocket = null; + + const context = canvas.getContext('2d'); + context.clearRect(0, 0, canvas.width, canvas.height); +} + window.WASM_PONG = {} window.WASM_PONG.width = width window.WASM_PONG.height = height +window.WASM_PONG.createOnlineSession = () => { + resetRequested = true; + fetch("http://localhost:4000/create_session", {method: 'POST'}).then(res => res.json()).then(session => { + console.log("Created session:") + console.log(session) + networkSession = session + const session_display_tag = document.getElementById("network_session"); + session_display_tag.style.display = 'block'; + session_display_tag.innerHTML = JSON.stringify(session) + + websocket = new WebSocket("ws://localhost:4000") + websocket.onmessage = (event) => { + console.log(event) + } + }).catch(err => { + console.error(`Failed to create session: ${err}`) + }) +} + window.WASM_PONG.pauseGame = () => { paused = true; document.getElementById("pause-btn").disabled = true;