From 67f0e397779d09f891d7a49969ef6cc184328a45 Mon Sep 17 00:00:00 2001 From: Araozu Date: Tue, 21 May 2024 09:09:47 -0500 Subject: [PATCH] Minimal websocket conection to backend --- src/assets/error.svg | 6 ++++ src/assets/loading.svg | 6 ++++ src/index.tsx | 2 ++ src/pages/Lobby.tsx | 77 ++++++++++++++++++++++++++++++++++++++++++ src/utils.ts | 4 +-- 5 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/assets/error.svg create mode 100644 src/assets/loading.svg create mode 100644 src/pages/Lobby.tsx diff --git a/src/assets/error.svg b/src/assets/error.svg new file mode 100644 index 0000000..e2e0b35 --- /dev/null +++ b/src/assets/error.svg @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/src/assets/loading.svg b/src/assets/loading.svg new file mode 100644 index 0000000..a536af8 --- /dev/null +++ b/src/assets/loading.svg @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx index 64783a4..eef1653 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -4,6 +4,7 @@ import {HashRouter, Route} from "@solidjs/router"; import "./index.css"; import {Index} from "./pages/Index"; +import {Lobby} from "./pages/Lobby"; const root = document.getElementById("root"); @@ -15,6 +16,7 @@ render( () => ( + ), root!, diff --git a/src/pages/Lobby.tsx b/src/pages/Lobby.tsx new file mode 100644 index 0000000..597d7f8 --- /dev/null +++ b/src/pages/Lobby.tsx @@ -0,0 +1,77 @@ +import { Show, createSignal, onMount } from "solid-js"; +import LoadingIcon from "../assets/loading.svg"; +import ErrorIcon from "../assets/error.svg"; + +enum LobbyStatus { + Connecting, + Connected, + Disconnected, + Error, +} + +export function Lobby() { + const [status, setStatus] = createSignal(LobbyStatus.Connecting); + let ws: WebSocket|null = null; + + const lobbyConnect = async() => { + ws = new WebSocket(`${import.meta.env.VITE_WS_URL}/lobby/connect`); + + ws.addEventListener("open", () => { + console.log("Connection open!"); + setStatus(LobbyStatus.Connected); + }); + + ws.addEventListener("message", (ev) => { + console.log("message from ws!"); + console.log(ev); + }); + + ws.addEventListener("error", (ev) => { + console.error(ev); + setStatus(LobbyStatus.Error); + }); + + ws.addEventListener("close", () => { + console.log("connection closed"); + }); + }; + + const send = () => { + if (ws === null) { + setStatus(LobbyStatus.Disconnected); + return; + } + + const message = "hello SEKAI"; + ws.send(message); + }; + + onMount(lobbyConnect); + + return ( +
+

+ Lobby +

+ + +

+ ... + Connecting to the lobby +

+
+ + +

+ ... + Error connecting to the lobby +

+
+ + + + + +
+ ); +} diff --git a/src/utils.ts b/src/utils.ts index 083f03f..298471c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,7 @@ import axios from "axios"; export let backend = axios.create({ - baseURL: `${import.meta.env.VITE_BACKEND_URL}/api`, + baseURL: `${import.meta.env.VITE_BACKEND_URL}`, headers: { "Authorization": `Bearer ${localStorage.getItem("UserId") ?? ""}`, }, @@ -9,7 +9,7 @@ export let backend = axios.create({ export function set_auth_token(token: string) { backend = axios.create({ - baseURL: `${import.meta.env.VITE_BACKEND_URL}/api`, + baseURL: `${import.meta.env.VITE_BACKEND_URL}`, headers: { "Authorization": `Bearer ${token}`, },