mirror of
https://github.com/docmost/docmost.git
synced 2026-08-29 01:37:05 +08:00
fix room exit
This commit is contained in:
@@ -4,6 +4,10 @@ import { ExcalidrawFollowPayload } from '../types/excalidraw.types';
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ExcalidrawCollabService {
|
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(
|
async handleJoinRoom(
|
||||||
client: Socket,
|
client: Socket,
|
||||||
server: Server,
|
server: Server,
|
||||||
@@ -11,6 +15,12 @@ export class ExcalidrawCollabService {
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await client.join(roomId);
|
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();
|
const sockets = await server.in(roomId).fetchSockets();
|
||||||
|
|
||||||
if (sockets.length <= 1) {
|
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(
|
handleServerBroadcast(
|
||||||
client: Socket,
|
client: Socket,
|
||||||
roomId: string,
|
roomId: string,
|
||||||
@@ -68,7 +98,10 @@ export class ExcalidrawCollabService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async handleDisconnecting(client: Socket, server: Server): Promise<void> {
|
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(
|
const otherClients = (await server.in(roomId).fetchSockets()).filter(
|
||||||
(socket) => socket.id !== client.id,
|
(socket) => socket.id !== client.id,
|
||||||
);
|
);
|
||||||
@@ -76,7 +109,7 @@ export class ExcalidrawCollabService {
|
|||||||
const isFollowRoom = roomId.startsWith('follow@');
|
const isFollowRoom = roomId.startsWith('follow@');
|
||||||
|
|
||||||
if (!isFollowRoom && otherClients.length > 0) {
|
if (!isFollowRoom && otherClients.length > 0) {
|
||||||
client.broadcast.to(roomId).emit(
|
server.to(roomId).emit(
|
||||||
'room-user-change',
|
'room-user-change',
|
||||||
otherClients.map((socket) => socket.id),
|
otherClients.map((socket) => socket.id),
|
||||||
);
|
);
|
||||||
@@ -87,5 +120,8 @@ export class ExcalidrawCollabService {
|
|||||||
server.to(socketId).emit('broadcast-unfollow');
|
server.to(socketId).emit('broadcast-unfollow');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean up tracking
|
||||||
|
this.socketRooms.delete(client.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,11 +88,15 @@ export class WsGateway
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeMessage('leave-room')
|
@SubscribeMessage('leave-room')
|
||||||
handleLeaveRoom(
|
async handleLeaveRoom(
|
||||||
@ConnectedSocket() client: Socket,
|
@ConnectedSocket() client: Socket,
|
||||||
@MessageBody() roomName: string,
|
@MessageBody() roomId: string,
|
||||||
): void {
|
): Promise<void> {
|
||||||
client.leave(roomName);
|
await this.excalidrawCollabService.handleLeaveRoom(
|
||||||
|
client,
|
||||||
|
this.server,
|
||||||
|
roomId,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeMessage('server-broadcast')
|
@SubscribeMessage('server-broadcast')
|
||||||
|
|||||||
Reference in New Issue
Block a user