From 54fe2f2914b8a7babc61bee03b1ddaed087533a1 Mon Sep 17 00:00:00 2001 From: koomin1227 Date: Tue, 5 Dec 2023 00:16:12 +0900 Subject: [PATCH] =?UTF-8?q?[BE]=20Feat=20:=20=EB=AF=B8=20=EC=9D=B8?= =?UTF-8?q?=EC=A6=9D=20=EC=9C=A0=EC=A0=80=EC=9D=B8=20=EA=B2=BD=EC=9A=B0=20?= =?UTF-8?q?=EC=97=B0=EA=B2=B0=EC=9D=B4=20=EB=81=8A=EA=B8=B0=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/chat/chats.gateway.ts | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/BE/src/chat/chats.gateway.ts b/BE/src/chat/chats.gateway.ts index f5bc787..7dff93d 100644 --- a/BE/src/chat/chats.gateway.ts +++ b/BE/src/chat/chats.gateway.ts @@ -7,24 +7,34 @@ import { WebSocketGateway, WebSocketServer, } from '@nestjs/websockets'; -import { Server, Websocket } from 'ws'; +import { Server, WebSocket } from 'ws'; import { ChatService } from './chat.service'; import { ChatDto } from './dto/chat.dto'; import { Logger } from '@nestjs/common'; +interface ChatWebSocket extends WebSocket { + roomId: number; + userId: string; +} @WebSocketGateway({ path: 'chats', }) export class ChatsGateway implements OnGatewayConnection, OnGatewayDisconnect { @WebSocketServer() server: Server; - private rooms = new Map>(); + private rooms = new Map>(); private readonly logger = new Logger('ChatsGateway'); constructor(private readonly chatService: ChatService) {} - handleConnection(client: Websocket) { - this.logger.debug(`on connnect : ${client}`, 'ChatsGateway'); + handleConnection(client: ChatWebSocket, ...args) { + const { authorization } = args[0].headers; + const userId = this.chatService.validateUser(authorization); + if (userId === null) { + client.close(1008, '토큰이 유효하지 않습니다.'); + } + client.userId = userId; + this.logger.debug(`[${userId}] on connect`, 'ChatsGateway'); } - handleDisconnect(client: Websocket) { + handleDisconnect(client: ChatWebSocket) { if (client.roomId) { const roomId = client.roomId; const room = this.rooms.get(roomId); @@ -34,7 +44,7 @@ export class ChatsGateway implements OnGatewayConnection, OnGatewayDisconnect { } } this.logger.debug( - `on disconnect : ${client}, left rooms : ${Array.from( + `[${client.userId}] on disconnect => left rooms : ${Array.from( this.rooms.keys(), )}`, 'ChatsGateway', @@ -44,10 +54,10 @@ export class ChatsGateway implements OnGatewayConnection, OnGatewayDisconnect { @SubscribeMessage('send-message') async sendMessage( @MessageBody() message: ChatDto, - @ConnectedSocket() client: Websocket, + @ConnectedSocket() client: ChatWebSocket, ) { this.logger.debug( - `[send message] room ID: ${message['room_id']} , sender: ${message['sender']}, message: ${message['message']}`, + `[${client.userId}] send message { room ID: ${message['room_id']} , sender: ${message['sender']}, message: ${message['message']} }`, ); const room = this.rooms.get(message['room_id']); const sender = message['sender']; @@ -66,19 +76,22 @@ export class ChatsGateway implements OnGatewayConnection, OnGatewayDisconnect { @SubscribeMessage('join-room') joinRoom( @MessageBody() message: object, - @ConnectedSocket() client: Websocket, + @ConnectedSocket() client: ChatWebSocket, ) { const roomId = message['room_id']; client.roomId = roomId; if (this.rooms.has(roomId)) this.rooms.get(roomId).add(client); else this.rooms.set(roomId, new Set([client])); - this.logger.debug(`join room : ${roomId}`, 'ChatsGateway'); + this.logger.debug( + `[${client.userId}] join room : ${roomId}`, + 'ChatsGateway', + ); } @SubscribeMessage('leave-room') leaveRoom( @MessageBody() message: object, - @ConnectedSocket() client: Websocket, + @ConnectedSocket() client: ChatWebSocket, ) { const roomId = message['room_id']; const room = this.rooms.get(roomId);