Skip to content

Commit

Permalink
[BE] Feat : 미 인증 유저인 경우 연결이 끊기도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
koomin1227 committed Dec 4, 2023
1 parent 5d6314c commit 54fe2f2
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions BE/src/chat/chats.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number, Set<Websocket>>();
private rooms = new Map<number, Set<ChatWebSocket>>();
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);
Expand All @@ -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',
Expand All @@ -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'];
Expand All @@ -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);
Expand Down

0 comments on commit 54fe2f2

Please sign in to comment.