Authenticate with the server

master
Araozu 2024-05-25 16:26:29 -05:00
parent 6b45296293
commit 800fa62ec6
1 changed files with 43 additions and 8 deletions

View File

@ -1,6 +1,7 @@
import { Show, createSignal, onCleanup, onMount } from "solid-js"; import { Show, createSignal, onCleanup, onMount } from "solid-js";
import LoadingIcon from "../assets/loading.svg"; import LoadingIcon from "../assets/loading.svg";
import ErrorIcon from "../assets/error.svg"; import ErrorIcon from "../assets/error.svg";
import ConnectedIcon from "../assets/ok.svg";
import { userIdKey } from "./Index"; import { userIdKey } from "./Index";
import { useNavigate } from "@solidjs/router"; import { useNavigate } from "@solidjs/router";
@ -12,6 +13,11 @@ enum LobbyStatus {
Error, Error,
} }
type LobbyMsg = {
action: string,
value: string,
}
const connectionRetryInterval = 5000; const connectionRetryInterval = 5000;
export function Lobby() { export function Lobby() {
@ -31,20 +37,21 @@ export function Lobby() {
}; };
const onWsOpen = () => { const onWsOpen = () => {
console.log("Connection open!"); console.log("Connection open. Authenticating...");
setStatus(LobbyStatus.Connected); setStatus(LobbyStatus.Authenticating);
// The first message must be authenticating with the server // The first message must be authenticating with the server
const userId = localStorage.getItem(userIdKey); const userId = localStorage.getItem(userIdKey);
if (userId === null) { if (userId === null) {
// Redirect to home page // Redirect to home page
ws?.close(); ws?.close(1000);
navigate("/"); navigate("/");
return; return;
} }
// Send an auth message to the server // Send an auth message to the server
// The server will respond with either "authenticated" or "unauthenticated"
ws!.send(JSON.stringify({ ws!.send(JSON.stringify({
action: "auth", action: "auth",
value: userId, value: userId,
@ -52,8 +59,19 @@ export function Lobby() {
}; };
const onWsMessage = (ev: MessageEvent) => { const onWsMessage = (ev: MessageEvent) => {
console.log("message from ws!"); const data = JSON.parse(ev.data) as LobbyMsg;
console.log(ev.data);
switch (data.action) {
case "auth": {
if (data.value === "authenticated") {
setStatus(LobbyStatus.Connected);
} else if (data.value === "unauthenticated") {
ws?.close(1000);
navigate("/");
}
break;
}
}
}; };
const onWsError = (ev: Event) => { const onWsError = (ev: Event) => {
@ -62,7 +80,12 @@ export function Lobby() {
setStatus(LobbyStatus.Error); setStatus(LobbyStatus.Error);
}; };
const onWsClose = () => { const onWsClose = (ev: CloseEvent) => {
if (ev.code === 1000) {
// The connection was normally closed
return;
}
// If the previous state is "Error", we don't set // If the previous state is "Error", we don't set
// the state to Disconnected // the state to Disconnected
if (status() !== LobbyStatus.Error) { if (status() !== LobbyStatus.Error) {
@ -85,7 +108,8 @@ export function Lobby() {
onMount(lobbyConnect); onMount(lobbyConnect);
onCleanup(() => { onCleanup(() => {
ws?.close(); console.log("lobby cleanup");
ws?.close(1000);
}); });
return ( return (
@ -94,6 +118,12 @@ export function Lobby() {
Lobby Lobby
</h1> </h1>
<Show when={status() === LobbyStatus.Disconnected}>
<p>
<img class="inline-block h-6 animate-spin" src={LoadingIcon} alt="..." />
Disconected. Attempting reconnection in {connectionRetryInterval / 1000} seconds.
</p>
</Show>
<Show when={status() === LobbyStatus.Connecting}> <Show when={status() === LobbyStatus.Connecting}>
<p> <p>
<img class="inline-block h-6 animate-spin" src={LoadingIcon} alt="..." /> <img class="inline-block h-6 animate-spin" src={LoadingIcon} alt="..." />
@ -103,7 +133,7 @@ export function Lobby() {
<Show when={status() === LobbyStatus.Authenticating}> <Show when={status() === LobbyStatus.Authenticating}>
<p> <p>
<img class="inline-block h-6 animate-spin" src={LoadingIcon} alt="..." /> <img class="inline-block h-6 animate-spin" src={LoadingIcon} alt="..." />
Logging in to the server Authenticating with the server
</p> </p>
</Show> </Show>
@ -115,6 +145,11 @@ export function Lobby() {
</Show> </Show>
<Show when={status() === LobbyStatus.Connected}> <Show when={status() === LobbyStatus.Connected}>
<p>
<img class="inline-block h-6" src={ConnectedIcon} alt="..." />
Connected
</p>
<button class="bg-cyan-500 p-2 rounded text-white" onClick={send}>Send message</button> <button class="bg-cyan-500 p-2 rounded text-white" onClick={send}>Send message</button>
</Show> </Show>