From dbc98d221183ee34fc984373b6452d84276a7428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A4=EC=A4=80=EC=88=98?= <99115509+hoheesu@users.noreply.github.com> Date: Tue, 7 Jan 2025 18:39:15 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EA=B2=8C=EC=9E=84=EB=B0=A9=20=ED=87=B4?= =?UTF-8?q?=EC=9E=A5=20=EC=8B=9C=20=EC=9B=B9=EC=86=8C=EC=BC=93=20=EC=A2=85?= =?UTF-8?q?=EB=A3=8C=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat/chat.gateway.ts | 31 ++++++++++++++++++------------- src/gameRoom/gameRoom.service.ts | 3 ++- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/chat/chat.gateway.ts b/src/chat/chat.gateway.ts index 18f8249..ac388db 100644 --- a/src/chat/chat.gateway.ts +++ b/src/chat/chat.gateway.ts @@ -50,15 +50,21 @@ export class ChatGateway async handleDisconnect(client: Socket) { console.log(`Client disconnected: ${client.id}`); - const roomId = await this.gameRoomService.getRoomIdByClient(client.id); - const token = client.handshake.auth.token.replace('Bearer ', ''); - const decoded: any = jwt.verify(token, process.env.JWT_SECRET); - const userId = Number(decoded?.userId); + + // 1) userId를 client.data.userId 로 가져옴 + const userId = client.data.userId; + + // 2) DB에서 userId를 이용해 어느 방에 있었는지 찾기 + const roomId = await this.gameRoomService.getRoomIdByClient( + userId.toString(), + ); + if (roomId) { await this.gameRoomService.leaveRoom(roomId, userId); this.server.to(roomId.toString()).emit('message', { sender: 'System', - message: `User ${client.id} has disconnected.`, + // 소켓 식별자는 client.id, 그러나 실제 "유저명"을 보여주려면 userId를 써도 됨 + message: `User ${userId} has disconnected.`, }); } } @@ -114,20 +120,19 @@ export class ChatGateway async handleLeaveRoom(client: Socket, payload: { roomId: number }) { const { roomId } = payload; - const token = client.handshake.auth.token.replace('Bearer ', ''); - const decoded: any = jwt.verify(token, process.env.JWT_SECRET); - const userId = Number(decoded?.userId); + // const token = client.handshake.auth.token.replace('Bearer ', ''); + // const decoded: any = jwt.verify(token, process.env.JWT_SECRET); + // const userId = Number(decoded?.userId); + const userId = client.data.userId; + console.log(userId, ' want to leave', roomId, 'room'); - try { + if (roomId) { await this.gameRoomService.leaveRoom(roomId, userId); this.server.to(roomId.toString()).emit('message', { sender: 'System', - message: `User ${userId} left the room.`, + message: `User ${userId} has disconnected.`, }); - client.leave(roomId.toString()); - } catch (error) { - client.emit('error', { message: error.message }); } } } diff --git a/src/gameRoom/gameRoom.service.ts b/src/gameRoom/gameRoom.service.ts index dc8e781..7269f79 100644 --- a/src/gameRoom/gameRoom.service.ts +++ b/src/gameRoom/gameRoom.service.ts @@ -93,6 +93,7 @@ export class GameRoomService { const currentCount = await this.gameRoomUserRepository.count({ where: { roomId }, }); + if (currentCount >= room.maxPlayers) { throw new BadRequestException('Room is full'); } @@ -186,7 +187,7 @@ export class GameRoomService { async getRoomIdByClient(userId: string): Promise { const user = await this.gameRoomUserRepository.findOne({ - where: { userId: +userId }, + where: { userId: +userId }, // 정수 변환 }); return user ? user.roomId : null; }