diff --git a/server/src/http.rs b/server/src/http.rs index f878834..ac849af 100644 --- a/server/src/http.rs +++ b/server/src/http.rs @@ -160,10 +160,11 @@ async fn handle_session_join(session_manager: &Arc> let body = read_json_body::(&mut req).await; let session_join_res = locked.join_session(body.session_id, Player {id: addr.to_string()}).await; if let Err(e) = session_join_res { + eprintln!("Failed to join session: {:?}", e); return Ok(Response::builder().status(StatusCode::INTERNAL_SERVER_ERROR).body(Body::from(e)).unwrap()); } let serialized = json!(session_join_res.unwrap()); - return Ok(Response::new(Body::from(serialized.to_string()))) + return build_success_res(&serialized.to_string()); } async fn handle_event_write(session_manager: &Arc>, mut req: Request) -> Result, Infallible> { diff --git a/www/index.html b/www/index.html index c3374ef..3b3a74c 100644 --- a/www/index.html +++ b/www/index.html @@ -25,6 +25,10 @@ + + diff --git a/www/index.js b/www/index.js index 290f376..f9b5858 100644 --- a/www/index.js +++ b/www/index.js @@ -20,6 +20,7 @@ let actions = []; let networkSession = null; let websocket = null; +let isHost = true; const renderLoop = () => { if (resetRequested) { @@ -38,7 +39,7 @@ const renderLoop = () => { const tick = () => { field.tick(actions); - if (networkSession) { + if (networkSession && isHost) { sendEvents() } render(); @@ -46,7 +47,7 @@ const tick = () => { const sendEvents = () => { let objects = JSON.parse(field.objects()); - let events = {session_id: networkSession.hash, events: objects.map(o => ({session_id: networkSession.hash, topic: 'move', msg: JSON.stringify(o)}))}; + let events = {session_id: networkSession.hash, events: objects.map(o => ({session_id: networkSession.hash, topic: 'move', msg: JSON.stringify({...o, session_id: networkSession.hash})}))}; websocket.send(JSON.stringify(events)); } @@ -62,6 +63,7 @@ const reset = () => { field = FieldWrapper.new(); networkSession = null; + isHost = true; if (websocket) { websocket.close(); } @@ -81,6 +83,28 @@ window.WASM_PONG.createOnlineSession = () => { console.log("Created session:") console.log(session) networkSession = session + isHost = true; + 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) + } + waitForWebsocket(10, renderLoop) + }).catch(err => { + console.error(`Failed to create session: ${err}`) + }) +} + +window.WASM_PONG.joinOnlineSession = () => { + resetRequested = true; + const sessionId = document.getElementById('join-online-input').value + fetch(`http://localhost:4000/join_session`, {method: 'POST', body: JSON.stringify({session_id: sessionId})}).then(res => res.json()).then(({data: session}) => { + console.log("Joined 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)