From 7d4429557410181a558cec59a60bb7342909316c Mon Sep 17 00:00:00 2001 From: yubinquitous <65652094+yubinquitous@users.noreply.github.com> Date: Mon, 6 May 2024 19:28:14 +0900 Subject: [PATCH] =?UTF-8?q?[FIX]=20cors=20=ED=95=B4=EA=B2=B0=20(#122)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: rank가 0부터 시작하므로 1을 추가하여 프론트로 반환 #8 * [FEAT] 랭킹 조회 시 DB 정렬 로직 추가 * [FEAT] update BE-config * [FEAT] update 42 env * [FEAT] health check api * [CHORE] BE-config 업데이트 및 health api 수정 * [FIX] mkcert 파일 httpsOptions 삭제 * [FIX] 중복로그인 시 에러 반환 * [FIX] mute 시간 10->30초로 변경 * [CHORE] TODO 주석 삭제 * [FIX] socket secure 삭제 및 중복로그인 에러 수정 * [FIX] 중복로그인 에러 메시지 WS_DUPLICATE_LOGIN_ERROR 로 수정 * [FIX] cors 에러 해결 --- src/channels/channels.gateway.ts | 6 ++++-- src/common/adapter/socket-io.adapter.ts | 1 + src/game/game.gateway.ts | 9 ++++----- src/main.ts | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/channels/channels.gateway.ts b/src/channels/channels.gateway.ts index 2f4e1ce..a1271cc 100644 --- a/src/channels/channels.gateway.ts +++ b/src/channels/channels.gateway.ts @@ -19,10 +19,9 @@ import { import Redis from 'ioredis'; import { Server } from 'socket.io'; import { UserStatus } from 'src/common/enum'; -import { EVENT_USER_STATUS } from 'src/common/events'; +import { EVENT_ERROR, EVENT_USER_STATUS } from 'src/common/events'; import { WSBadRequestException } from 'src/common/exception/custom-exception'; import { WsFilter } from 'src/common/exception/custom-ws-exception.filter'; -import { WS_DUPLICATE_LOGIN_ERROR } from 'src/common/exception/error-code'; import { FriendsRepository } from 'src/friends/friends.repository'; import { User } from 'src/user-repository/entities/user.entity'; import { UsersRepository } from 'src/user-repository/users.repository'; @@ -74,6 +73,9 @@ export class ChannelsGateway console.log('socket이 이미 존재합니다.'); const socket = await this.isSocketConnected(user.channelSocketId); if (socket) { + this.server + .to(socket.id) + .emit(EVENT_ERROR, 'WS_DUPLICATE_LOGIN_ERROR'); socket.disconnect(); } else { await this.usersRepository.update(user.id, { diff --git a/src/common/adapter/socket-io.adapter.ts b/src/common/adapter/socket-io.adapter.ts index 2b7e4c1..289deed 100644 --- a/src/common/adapter/socket-io.adapter.ts +++ b/src/common/adapter/socket-io.adapter.ts @@ -28,6 +28,7 @@ export class SocketIoAdapter extends IoAdapter { new RegExp( `/^http:\/\/192\.168\.1\.([1-9]|[1-9]\d):${clientPort}$/`, ), + `https://tscenping.shop`, ], }; diff --git a/src/game/game.gateway.ts b/src/game/game.gateway.ts index 6f07792..dce917f 100644 --- a/src/game/game.gateway.ts +++ b/src/game/game.gateway.ts @@ -16,7 +16,6 @@ import { } from '@nestjs/websockets'; import Redis from 'ioredis'; import { Server, Socket } from 'socket.io'; -import { WS_DUPLICATE_LOGIN_ERROR } from 'src/common/exception/error-code'; import { ChannelsGateway } from '../channels/channels.gateway'; import { SocketWithAuth } from '../common/adapter/socket-io.adapter'; import { K } from '../common/constants'; @@ -85,16 +84,16 @@ export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect { const user = client.user; if (!user || !client.id) return client.disconnect(); else if (user.gameSocketId) { - console.log('game socket 갈아끼운다 ~?!'); + console.log('game socket이 이미 존재합니다.'); const socket = this.userIdToClient.get(user.id); if (socket) { const result = await this.isSocketConnected(socket); if (result) { // 이미 로그인된 유저의 경우, 기존 소켓을 disconnect한다. this.server - .to(socket.id) - .emit(EVENT_ERROR, WS_DUPLICATE_LOGIN_ERROR); - socket.disconnect(); + .to(result.id) + .emit(EVENT_ERROR, 'WS_DUPLICATE_LOGIN_ERROR'); + result.disconnect(); } else this.userIdToClient.delete(user.id); } else { await this.usersRepository.update(user.id, { diff --git a/src/main.ts b/src/main.ts index d695ef9..77b6038 100644 --- a/src/main.ts +++ b/src/main.ts @@ -19,7 +19,7 @@ async function bootstrap() { app.enableCors({ credentials: true, // origin: 'https://localhost:3003', - origin: true, + origin: ['http://localhost:3003', 'https://tscenping.shop'], }); app.use(cookieParser());