generated from pure_sagacity/turborepo-starter-project
everything but the leave works yall
This commit is contained in:
@@ -8,6 +8,14 @@ export class Clients {
|
||||
this.clients = new Map();
|
||||
}
|
||||
|
||||
get clientList(): ClientList {
|
||||
return this.clients;
|
||||
}
|
||||
|
||||
get roomList(): string[] {
|
||||
return Array.from(this.clients.keys());
|
||||
}
|
||||
|
||||
userInRoom(username: string): boolean {
|
||||
this.clients.forEach((room) => {
|
||||
if (room.some((client) => client.username === username)) {
|
||||
@@ -18,10 +26,6 @@ export class Clients {
|
||||
return false;
|
||||
}
|
||||
|
||||
get clientList(): ClientList {
|
||||
return this.clients;
|
||||
}
|
||||
|
||||
addClient(roomId: string, username: string, ws: ElysiaWS) {
|
||||
if (!this.clients.has(roomId)) {
|
||||
this.clients.set(roomId, []);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import Elysia from "elysia";
|
||||
import type { ElysiaWS } from "elysia/ws";
|
||||
import { ClientMessage, ClientMessageSchema } from "./types";
|
||||
import { ClientMessage, ClientMessageSchema, ServerMessage } from "./types";
|
||||
import { Clients } from "./classes";
|
||||
import { db, message as messageTable } from "@/lib/db";
|
||||
|
||||
@@ -57,6 +57,13 @@ async function handleMessage(ws: ElysiaWS, message: ClientMessage) {
|
||||
|
||||
break;
|
||||
}
|
||||
case "rooms": {
|
||||
ws.send({
|
||||
type: "rooms",
|
||||
rooms: clients.roomList,
|
||||
} as ServerMessage);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,6 +33,9 @@ const ClientMessageSchema = t.Union([
|
||||
roomId: t.String(),
|
||||
// No need for username as we can just directly send the sync through the WS
|
||||
}),
|
||||
t.Object({
|
||||
type: t.Literal("rooms"),
|
||||
}),
|
||||
]);
|
||||
|
||||
// Server -> Client(s)
|
||||
@@ -55,6 +58,10 @@ const ServerMessageSchema = t.Union([
|
||||
roomId: t.String(),
|
||||
messages: t.Array(MessageSchema),
|
||||
}),
|
||||
t.Object({
|
||||
type: t.Literal("rooms"),
|
||||
rooms: t.Array(t.String()),
|
||||
}),
|
||||
]);
|
||||
|
||||
type ClientList = Map<
|
||||
|
||||
Reference in New Issue
Block a user