websocket-chat / src /services /RoomManager.ts
harsh-dev's picture
rename
7eec780 unverified
import WebSocket from 'ws'
export class RoomManager {
// Map<RoomId, Map<Username, WebSocket>>
private rooms: Map<string, Map<string, WebSocket>>
constructor() {
this.rooms = new Map()
}
/**
* Adds a user to a specific room.
* Creates the room if it doesn't exist.
*/
joinRoom(roomId: string, username: string, socket: WebSocket): void {
if (!this.rooms.has(roomId)) {
this.rooms.set(roomId, new Map())
}
this.rooms.get(roomId)?.set(username, socket)
}
renameUser(
roomId: string,
username: string,
newUsername: string,
socket: WebSocket
): boolean {
const room = this.rooms.get(roomId)
if (!room) return false
if (!room.has(username)) return false
if (room.has(newUsername)) return false
// Remove old username
room.delete(username)
// Add new username with same socket
room.set(newUsername, socket)
return true
}
/**
* Removes a user from a room.
* Cleans up the room if it becomes empty.
*/
leaveRoom(roomId: string, username: string): boolean {
const room = this.rooms.get(roomId)
if (room) {
const deleted = room.delete(username)
if (room.size === 0) {
this.rooms.delete(roomId)
}
return deleted
}
return false
}
/**
* Checks if a room exists.
*/
hasRoom(roomId: string): boolean {
return this.rooms.has(roomId)
}
/**
* Sends a message to all users in a specific room.
*/
broadcast(roomId: string, message: string): void {
const room = this.rooms.get(roomId)
if (!room) return
for (const client of room.values()) {
if (client.readyState === WebSocket.OPEN) {
client.send(message)
}
}
}
}