join session

This commit is contained in:
Thilo Behnke
2022-06-01 22:04:44 +02:00
parent 7424030c80
commit 3b68e58bb7
3 changed files with 32 additions and 3 deletions

View File

@@ -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> {

View File

@@ -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>

View File

@@ -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)