mirror of
https://github.com/thilo-behnke/wasm-pong.git
synced 2026-07-12 19:19:16 +00:00
join session
This commit is contained in:
@@ -160,10 +160,11 @@ async fn handle_session_join(session_manager: &Arc<Mutex<CachingSessionManager>>
|
||||
let body = read_json_body::<SessionJoinDto>(&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<Mutex<CachingSessionManager>>, mut req: Request<Body>) -> Result<Response<Body>, Infallible> {
|
||||
|
||||
@@ -25,6 +25,10 @@
|
||||
<button id="online-btn" onclick="WASM_PONG.createOnlineSession()">
|
||||
Create Online Game
|
||||
</button>
|
||||
<input type="text" id="join-online-input"/>
|
||||
<button id="join-online-btn" onclick="WASM_PONG.joinOnlineSession()">
|
||||
Join Online Game
|
||||
</button>
|
||||
<button id="pause-btn" onclick="WASM_PONG.pauseGame()">
|
||||
Pause
|
||||
</button>
|
||||
|
||||
28
www/index.js
28
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)
|
||||
|
||||
Reference in New Issue
Block a user