Skip to content

Commit

Permalink
app -> joining and leaving existing rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLaurens committed Aug 12, 2024
1 parent 7d87754 commit 8d42aef
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions frontend/src/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {atom} from "nanostores";
import {persistentAtom} from "@nanostores/persistent";

export const roomCode = persistentAtom("roomCode", undefined);
export const isConnected = atom(false);

class WebsocketManager {
private socket: WebSocket | null = null;
Expand Down Expand Up @@ -48,13 +49,13 @@ class WebsocketManager {

// request a new room
sendRequestRoom(this.socket);

break;
}
case "ROOM_CREATED": {
console.log("Room created", data.room);
let room = data.room;
roomCode.set(room.code);
isConnected.set(true);
break;
}
case "ROOM_EXISTS": {
Expand All @@ -65,8 +66,20 @@ class WebsocketManager {
break;
}
roomCode.set(data.code);
// TODO: join the room

sendJoinRoom(this.socket, data.code);
break;
}
case "ROOM_JOINED": {
console.log("Room joined", data);
isConnected.set(true);
break;
}
case "USER_JOINED": {
console.log("User joined", data);
break;
}
case "USER_LEFT": {
console.log("User left", data);
break;
}
}
Expand All @@ -90,4 +103,14 @@ function sendRoomExists(socket: WebSocket | null, code: string) {
}))
}

function sendJoinRoom(socket: WebSocket | null, code: string) {
if (!socket) return;
socket.send(JSON.stringify({
type: "JOIN_ROOM",
payload: {
code: code
}
}))
}

export default WebsocketManager;

0 comments on commit 8d42aef

Please sign in to comment.