From fa3cdfb8b3ed48e00718998c692fc2403ca251d8 Mon Sep 17 00:00:00 2001 From: Ungs Date: Sun, 24 Nov 2024 11:36:58 +0900 Subject: [PATCH 1/2] feat : server is full logic --- src/auth/auth.service.ts | 2 -- src/socket/socket.gateway.ts | 41 +++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index f489f2c..5e3acf4 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -56,8 +56,6 @@ export class AuthService { } async logout(@Req() request: Request): Promise { - const { id, provider } = request.session.user; - if (provider === 'guest') this.userService.deleteGuest(id); request.session.destroy(() => {}); return { success: true, message: 'Logout Success' }; } diff --git a/src/socket/socket.gateway.ts b/src/socket/socket.gateway.ts index 16db560..01fe1e4 100644 --- a/src/socket/socket.gateway.ts +++ b/src/socket/socket.gateway.ts @@ -138,48 +138,55 @@ export class SocketGateway [roomId: string]: NodeJS.Timeout; } = {}; - // 접속시 수행되는 코드 async handleConnection(@ConnectedSocket() client: any) { try { const isAuthenticated = await this.guard.validateClient(client); if (!isAuthenticated) { - client.disconnect(); // 인증 실패 시 연결 해제 + client.disconnect(); return; } - // 최대 연결 수 체크 - if (this.currentConnections >= this.MAX_CONNECTIONS) { + + this.currentConnections++; // 연결 수 증가 + + // 최대 연결 수 확인 + if (this.currentConnections > this.MAX_CONNECTIONS) { this.logger.warn( `Connection rejected - Max connections reached: ${client.id}`, ); client.emit('full', { message: '서버가 가득 찼습니다. 잠시 후 다시 시도해주세요.', }); - client.disconnect(); + setTimeout(() => client.disconnect(), 200); return; } - const user = client.request.session.user; - this.logger.log(`Client connected: ${client.id}`); + const user = client.request.session?.user; if (!user) { this.logger.warn(`Connection rejected - No user session: ${client.id}`); client.disconnect(); return; } - for (const key in this.user) { - if (this.user[key].id === user.id) { - this.server - .to(key) - .emit('alarm', { message: '이미 접속중인 유저입니다!' }); - this.handleDisconnect({ id: key }); - } + + // 중복 접속 확인 및 처리 + const existingClientId = Object.keys(this.user).find( + (key) => this.user[key].id === user.id, + ); + if (existingClientId) { + this.server + .to(existingClientId) + .emit('alarm', { message: '이미 접속중인 유저입니다!' }); + this.handleDisconnect({ id: existingClientId }); } - this.currentConnections++; // 연결 수 증가 + this.user[client.id] = await this.socketService.reloadUser(user.id); this.user[client.id].color = this.socketService.getColor(); - client.emit('list', this.user); + + client.emit('connected'); // 필요한 정보만 전달 + this.logger.log(`Client connected: ${client.id}`); } catch (error) { this.logger.error(`Connection error: ${error.message}`, error.stack); + client.emit('error', { message: '서버 오류가 발생했습니다.' }); } } @@ -237,7 +244,7 @@ export class SocketGateway } } - if (this.user[client.id].provider === 'guest') { + if (this.user[client.id] && this.user[client.id].provider === 'guest') { await this.socketService.deleteGuest(this.user[client.id].id); client.request.session.destroy(() => {}); } From 244aa337659b79443a130802b7fec3c2a0242225 Mon Sep 17 00:00:00 2001 From: Ungs Date: Sun, 24 Nov 2024 13:59:06 +0900 Subject: [PATCH 2/2] feat : emoticon --- src/socket/socket.gateway.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/socket/socket.gateway.ts b/src/socket/socket.gateway.ts index 01fe1e4..341e269 100644 --- a/src/socket/socket.gateway.ts +++ b/src/socket/socket.gateway.ts @@ -28,6 +28,12 @@ interface Chat { success?: boolean; } +interface Emoticon { + emoticonId: string; + userId: string; + roomId: string; +} + export class Game { constructor() { this.host = ''; // 호스트 @@ -403,6 +409,13 @@ export class SocketGateway } } + // Emoticon + @SubscribeMessage('emoticon') + handleEmoticon(@MessageBody() data: Emoticon) { + console.log('emoticon'); + this.server.to(data.roomId).emit('emoticon', data); + } + // 게임 방 생성 @SubscribeMessage('createRoom') async handleCreate(