From bce8487f0a126cf01091365dffb9490c5180087e Mon Sep 17 00:00:00 2001 From: Thilo Behnke Date: Thu, 23 Jun 2022 00:11:41 +0200 Subject: [PATCH] wip - issue with websocket / session events setup --- client/svelte-client/src/api/session.ts | 19 +++++++++++-------- client/svelte-client/src/store/model/event.ts | 9 ++++++++- client/svelte-client/src/store/session.ts | 2 ++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/client/svelte-client/src/api/session.ts b/client/svelte-client/src/api/session.ts index 226cb82..081add9 100644 --- a/client/svelte-client/src/api/session.ts +++ b/client/svelte-client/src/api/session.ts @@ -1,6 +1,6 @@ -import type {LocalSession, Session} from "../store/model/session"; +import type {LocalSession, NetworkSession, Session} from "../store/model/session"; import {SessionState, SessionType} from "../store/model/session"; -import type {SessionEventPayload} from "../store/model/event"; +import type {NetworkSessionEventPayload, SessionEventPayload} from "../store/model/event"; async function createLocalSession(): Promise { await new Promise((res) => { @@ -15,9 +15,10 @@ async function createLocalSession(): Promise { } } -async function createNetworkSession(): Promise { +async function createNetworkSession(): Promise { return fetch("/pong/api/create_session", {method: 'POST', headers: [['Content-Type', 'application/json']]}) .then(sessionResponseHandler) + .then(session => ({...session, type: SessionType.HOST}) as NetworkSession) .catch(err => { console.error(`Failed to create session: ${err}`); throw(err); @@ -28,33 +29,35 @@ function createJoinLink(sessionId: string): string { return `${window.location.origin}${window.location.pathname}?session_id=${sessionId}`; } -async function joinNetworkSession(sessionId): Promise { +async function joinNetworkSession(sessionId): Promise { return fetch("/pong/api/join_session", { method: 'POST', body: JSON.stringify({session_id: sessionId}), headers: [['Content-Type', 'application/json']] }) .then(sessionResponseHandler) + .then(session => ({...session, type: SessionType.PEER}) as NetworkSession) .catch(err => { console.error(`Failed to create session: ${err}`); throw(err); }); } -async function watchNetworkSession(sessionId): Promise { +async function watchNetworkSession(sessionId): Promise { return fetch("/pong/api/watch_session", { method: 'POST', body: JSON.stringify({session_id: sessionId}), headers: [['Content-Type', 'application/json']] }) .then(sessionResponseHandler) + .then(session => ({...session, type: SessionType.OBSERVER} as NetworkSession)) .catch(err => { console.error(`Failed to create session: ${err}`); throw(err); }); } -async function sessionResponseHandler(response: Response): Promise { +async function sessionResponseHandler(response: Response): Promise { if (!response.ok) { return response.text().then(text => { return Promise.reject(`${response.status}: ${text}`) @@ -63,7 +66,7 @@ async function sessionResponseHandler(response: Response): Promise { return response.json().then(({data}) => { console.debug(`session action result: ${JSON.stringify(data)}`) return data; - }).then((event: SessionEventPayload) => event.session); + }).then((event: NetworkSessionEventPayload) => event.session); } async function createEventWebsocket(session: Session): Promise { @@ -71,7 +74,7 @@ async function createEventWebsocket(session: Session): Promise { if (session.type === SessionType.LOCAL) { return rej("Websocket not allowed for local session!"); } - const url = `pong/ws?session_id=${session.session_id}&connection_type=${session.type.toLowerCase()}`; + const url = `/pong/ws?session_id=${session.session_id}&connection_type=${session.type.toLowerCase()}`; return createWebsocket(url); }) } diff --git a/client/svelte-client/src/store/model/event.ts b/client/svelte-client/src/store/model/event.ts index 5ff82ac..aedf2c0 100644 --- a/client/svelte-client/src/store/model/event.ts +++ b/client/svelte-client/src/store/model/event.ts @@ -1,5 +1,5 @@ import type {Input} from "../session"; -import type {Session} from "./session"; +import type {NetworkSession, Session} from "./session"; export type SessionEventPayload = { actor: {id: string}, @@ -8,6 +8,13 @@ export type SessionEventPayload = { session: Session } +export type NetworkSessionEventPayload = { + actor: {id: string}, + event_type: string, + reason: string, + session: NetworkSession +} + export type InputEventPayload = { session_id: string, inputs: Input[], diff --git a/client/svelte-client/src/store/session.ts b/client/svelte-client/src/store/session.ts index 6884398..c6609f0 100644 --- a/client/svelte-client/src/store/session.ts +++ b/client/svelte-client/src/store/session.ts @@ -63,6 +63,8 @@ const sessionEvents = (session: Session) => readable([], function(set) { set(get(events)); }, 0) + set(get(events)); + return () => { clearInterval(interval); }