cleanup 이벤트를 위조해서 보내면, 멀쩡히 회의 중인 다른 사람들의 방을 폭파시킬 수 있습니다.서버는 다음 두 가지 상황에서 자동으로 청소 로직을 돌려야 합니다.
방에 5명이 있다가 4명이 나가고, 마지막 1명이 disconnect 하거나 leaveRoom을 했을 때, 서버는 스스로 판단해야 합니다.
"어? 방금 나간 놈이 마지막이네? 그럼 이제 Mediasoup Router 닫고 메모리 비우자."
방장이 강제로 방을 폭파하는 기능이 있다면, 이때는 room:close 같은 비즈니스 로직 API를 호출하고, 그 내부에서 청소 메서드를 실행합니다.
cleanup은 API가 아니라 Service 내부의 Private Method여야 합니다.
// voice.gateway.ts (게이트웨이)
@SubscribeMessage('voice:leave')
async handleLeave(@ConnectedSocket() client: Socket) {
// 1. 유저 리소스 정리 (Transport, Producer, Consumer 닫기)
this.voiceService.removePeer(client.data.roomId, client.data.userId);
// 2. ⭐️ 자동 청소 트리거 ⭐️
// 유저가 떠난 후 방이 비었는지 체크합니다.
if (this.voiceService.isRoomEmpty(client.data.roomId)) {
// 3. 방이 비었으면 방 전체 리소스 정리 (Router 닫기)
this.voiceService.closeRoom(client.data.roomId);
console.log(`방(${client.data.roomId})이 비어서 삭제했습니다.`);
}
}