fix room exit

This commit is contained in:
Philipinho
2026-01-17 13:42:58 +00:00
parent efa52ea4c8
commit 14698ebb05
2 changed files with 46 additions and 6 deletions
@@ -4,6 +4,10 @@ import { ExcalidrawFollowPayload } from '../types/excalidraw.types';
@Injectable()
export class ExcalidrawCollabService {
// Track socket -> rooms mapping for disconnect handling
// (Socket.IO clears client.rooms before handleDisconnect runs)
private socketRooms = new Map<string, Set<string>>();
async handleJoinRoom(
client: Socket,
server: Server,
@@ -11,6 +15,12 @@ export class ExcalidrawCollabService {
): Promise<void> {
await client.join(roomId);
// Track room membership
if (!this.socketRooms.has(client.id)) {
this.socketRooms.set(client.id, new Set());
}
this.socketRooms.get(client.id).add(roomId);
const sockets = await server.in(roomId).fetchSockets();
if (sockets.length <= 1) {
@@ -25,6 +35,26 @@ export class ExcalidrawCollabService {
);
}
async handleLeaveRoom(
client: Socket,
server: Server,
roomId: string,
): Promise<void> {
await client.leave(roomId);
// Remove from tracking
this.socketRooms.get(client.id)?.delete(roomId);
// Notify remaining users
const sockets = await server.in(roomId).fetchSockets();
if (sockets.length > 0) {
server.in(roomId).emit(
'room-user-change',
sockets.map((socket) => socket.id),
);
}
}
handleServerBroadcast(
client: Socket,
roomId: string,
@@ -68,7 +98,10 @@ export class ExcalidrawCollabService {
}
async handleDisconnecting(client: Socket, server: Server): Promise<void> {
for (const roomId of Array.from(client.rooms)) {
// Use tracked rooms since client.rooms is empty by this point
const rooms = this.socketRooms.get(client.id) || new Set();
for (const roomId of rooms) {
const otherClients = (await server.in(roomId).fetchSockets()).filter(
(socket) => socket.id !== client.id,
);
@@ -76,7 +109,7 @@ export class ExcalidrawCollabService {
const isFollowRoom = roomId.startsWith('follow@');
if (!isFollowRoom && otherClients.length > 0) {
client.broadcast.to(roomId).emit(
server.to(roomId).emit(
'room-user-change',
otherClients.map((socket) => socket.id),
);
@@ -87,5 +120,8 @@ export class ExcalidrawCollabService {
server.to(socketId).emit('broadcast-unfollow');
}
}
// Clean up tracking
this.socketRooms.delete(client.id);
}
}
+8 -4
View File
@@ -88,11 +88,15 @@ export class WsGateway
}
@SubscribeMessage('leave-room')
handleLeaveRoom(
async handleLeaveRoom(
@ConnectedSocket() client: Socket,
@MessageBody() roomName: string,
): void {
client.leave(roomName);
@MessageBody() roomId: string,
): Promise<void> {
await this.excalidrawCollabService.handleLeaveRoom(
client,
this.server,
roomId,
);
}
@SubscribeMessage('server-broadcast')