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<
|
||||
|
||||
@@ -9,3 +9,7 @@ websocket:
|
||||
type: json
|
||||
data: "{}"
|
||||
auth: inherit
|
||||
|
||||
settings:
|
||||
timeout: 0
|
||||
keepAliveInterval: 0
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
info:
|
||||
name: join
|
||||
type: websocket
|
||||
seq: 3
|
||||
|
||||
websocket:
|
||||
url: ws://localhost:3000/realtime
|
||||
message:
|
||||
type: json
|
||||
data: |-
|
||||
{
|
||||
"type": "join",
|
||||
"username": "camila",
|
||||
"roomId": "67676767676767"
|
||||
}
|
||||
auth: inherit
|
||||
|
||||
settings:
|
||||
timeout: 0
|
||||
keepAliveInterval: 0
|
||||
@@ -0,0 +1,20 @@
|
||||
info:
|
||||
name: leave
|
||||
type: websocket
|
||||
seq: 6
|
||||
|
||||
websocket:
|
||||
url: ws://localhost:3000/realtime
|
||||
message:
|
||||
type: json
|
||||
data: |-
|
||||
{
|
||||
"type": "leave",
|
||||
"username": "camila",
|
||||
"roomId": "67676767676767"
|
||||
}
|
||||
auth: inherit
|
||||
|
||||
settings:
|
||||
timeout: 0
|
||||
keepAliveInterval: 0
|
||||
@@ -0,0 +1,28 @@
|
||||
info:
|
||||
name: message
|
||||
type: websocket
|
||||
seq: 5
|
||||
|
||||
websocket:
|
||||
url: ws://localhost:3000/realtime
|
||||
message:
|
||||
type: json
|
||||
data: |-
|
||||
{
|
||||
"type": "message",
|
||||
"username": "bob",
|
||||
"roomId": "67676767676767",
|
||||
"m": {
|
||||
"id": 1,
|
||||
"roomId": "67676767676767",
|
||||
"username": "bob",
|
||||
"content": "I alone am the honored one.",
|
||||
"timestamp": 1784349801729,
|
||||
"deleted": false
|
||||
}
|
||||
}
|
||||
auth: inherit
|
||||
|
||||
settings:
|
||||
timeout: 0
|
||||
keepAliveInterval: 0
|
||||
@@ -0,0 +1,18 @@
|
||||
info:
|
||||
name: rooms
|
||||
type: websocket
|
||||
seq: 4
|
||||
|
||||
websocket:
|
||||
url: ws://localhost:3000/realtime
|
||||
message:
|
||||
type: text
|
||||
data: |-
|
||||
{
|
||||
"type": "rooms"
|
||||
}
|
||||
auth: inherit
|
||||
|
||||
settings:
|
||||
timeout: 0
|
||||
keepAliveInterval: 0
|
||||
Reference in New Issue
Block a user