aboutsummaryrefslogtreecommitdiffstats
path: root/backend/src/config
diff options
context:
space:
mode:
authorLeonardo Bishop <me@leonardobishop.com>2023-11-09 00:17:03 +0000
committerLeonardo Bishop <me@leonardobishop.com>2023-11-09 00:17:03 +0000
commit1f101fe7cc5cc31c66146a1e226fa4bae805fdd4 (patch)
treee1d533f686741bf0c7a446ba8ada1ac66a2051ef /backend/src/config
parent28f705fc4aa2f3c62e7fe7d95557494bedcb8d1c (diff)
Refactor websockets
Diffstat (limited to 'backend/src/config')
-rw-r--r--backend/src/config/coop-session-store.ts73
-rw-r--r--backend/src/config/session-store.ts77
2 files changed, 73 insertions, 77 deletions
diff --git a/backend/src/config/coop-session-store.ts b/backend/src/config/coop-session-store.ts
new file mode 100644
index 0000000..09fef87
--- /dev/null
+++ b/backend/src/config/coop-session-store.ts
@@ -0,0 +1,73 @@
+import { CoopSession, SessionState } from "../model/session";
+import { CoopWebSocket } from "../model/websocket";
+
+const sessions: { [key: string]: CoopSession } = {};
+
+function makeid(length) {
+ let result = '';
+ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
+ const charactersLength = characters.length;
+ let counter = 0;
+ while (counter < length) {
+ result += characters.charAt(Math.floor(Math.random() * charactersLength));
+ counter += 1;
+ }
+ return result;
+}
+
+
+export const createNewSession = (host: CoopWebSocket): CoopSession => {
+ const id = makeid(10);
+ const session: CoopSession = {
+ id,
+ state: "waiting",
+ host: host,
+ clients: [],
+ };
+ sessions[id] = session;
+ return session;
+};
+
+export const setSessionState = (id: string, state: SessionState): void => {
+ if (!sessions[id]) {
+ return;
+ }
+
+ sessions[id].state = state;
+
+ if (state === "finished") {
+ cleanupSession(id);
+ }
+};
+
+export const setSessionHost = (id: string, client: CoopWebSocket): void => {
+ if (!sessions[id]) {
+ return;
+ }
+
+ sessions[id].host = client;
+};
+
+export const addSessionClient = (id: string, client: CoopWebSocket): void => {
+ if (!sessions[id]) {
+ return;
+ }
+
+ sessions[id]?.clients.push(client);
+};
+
+export const cleanupSession = (id: string): void => {
+ if (!sessions[id]) {
+ return;
+ }
+
+ sessions[id].host.close();
+
+ sessions[id].clients.forEach(client => client.close());
+
+ delete sessions[id];
+}
+
+export const getSession = (id: string): CoopSession => {
+ return sessions[id];
+}; \ No newline at end of file
diff --git a/backend/src/config/session-store.ts b/backend/src/config/session-store.ts
deleted file mode 100644
index 283031b..0000000
--- a/backend/src/config/session-store.ts
+++ /dev/null
@@ -1,77 +0,0 @@
-export type Session = {
- id: string;
- state: string;
- host?: string;
- clients: string[];
-};
-
-const sessions: { [key: string]: Session } = {};
-
-function makeid(length) {
- let result = '';
- const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
- const charactersLength = characters.length;
- let counter = 0;
- while (counter < length) {
- result += characters.charAt(Math.floor(Math.random() * charactersLength));
- counter += 1;
- }
- return result;
-}
-
-
-export const createNewSession = (): Session => {
- const id = makeid(10);
- const session = {
- id,
- state: "waiting",
- host: undefined,
- clients: [],
- };
- sessions[id] = session;
- return session;
-};
-
-export const setSessionState = (id: string, state: string): void => {
- if (!sessions[id]) {
- return;
- }
-
- sessions[id].state = state;
-};
-
-export const setSessionHost = (id: string, clientId: string): void => {
- if (!sessions[id]) {
- return;
- }
-
- sessions[id].host = clientId;
-};
-
-export const addSessionClient = (id: string, clientId: string): void => {
- if (!sessions[id]) {
- return;
- }
-
- sessions[id]?.clients.push(clientId);
-};
-
-export const cleanupSession = (id: string): void => {
- if (!sessions[id]) {
- return;
- }
-
-// if (sessions[id].host) {
-// sessions[id].host!.close();
-// }
-//
-// sessions[id].clients.forEach((client) => {
-// client.close();
-// });
-
- delete sessions[id];
-}
-
-export const getSession = (id: string): Session => {
- return sessions[id];
-}; \ No newline at end of file