Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] 중복로그인 에러 반환하도록 수정 #120

Merged
merged 14 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/channels/channels.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@ import Redis from 'ioredis';
import { Server } from 'socket.io';
import { UserStatus } from 'src/common/enum';
import { EVENT_USER_STATUS } from 'src/common/events';
import { WSBadRequestException } from 'src/common/exception/custom-exception';
import {
WSBadRequestException,
WSDuplicateLoginException,
} from 'src/common/exception/custom-exception';
import { WsFilter } from 'src/common/exception/custom-ws-exception.filter';
import { FriendsRepository } from 'src/friends/friends.repository';
import { User } from 'src/user-repository/entities/user.entity';
import { UsersRepository } from 'src/user-repository/users.repository';
import { SocketWithAuth } from '../common/adapter/socket-io.adapter';
import { WsAuthGuard } from '../common/guards/ws-auth.guard';
import { ChannelUsersRepository } from './channel-users.repository';
import { ChannelNameChangeResponseDto } from './dto/channel-name-change-response.dto';
import { ChannelNoticeResponseDto } from './dto/channel-notice.response.dto';
import { EventMessageOnDto } from './dto/event-message-on.dto';
import { User } from 'src/user-repository/entities/user.entity';
import { WsAuthGuard } from '../common/guards/ws-auth.guard';
import { SocketWithAuth } from '../common/adapter/socket-io.adapter';
import { ChannelNameChangeResponseDto } from './dto/channel-name-change-response.dto';

@WebSocketGateway({ namespace: 'channels' })
@UseGuards(WsAuthGuard)
Expand Down Expand Up @@ -73,7 +76,7 @@ export class ChannelsGateway
console.log('channel socket 갈아끼운다 ~?!');
const socket = await this.isSocketConnected(user.channelSocketId);
if (socket) {
socket.disconnect();
throw WSDuplicateLoginException();
} else {
await this.usersRepository.update(user.id, {
channelSocketId: null,
Expand Down
8 changes: 7 additions & 1 deletion src/common/exception/custom-exception.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { WsException } from '@nestjs/websockets';
import {
DB_QUERY_ERROR,
DB_UPDATE_FAILURE,
Expand All @@ -7,7 +8,6 @@ import {
WS_UNAUTHORIZED_ERROR,
WsErrorCode,
} from './error-code';
import { WsException } from '@nestjs/websockets';
export class customException extends Error {
readonly errorCode: ErrorCode;

Expand Down Expand Up @@ -53,6 +53,12 @@ export const WSBadRequestException = (message?: string): customWsException => {
return new customWsException(WS_BAD_REQUEST_ERROR, message);
};

export const WSDuplicateLoginException = (
message?: string,
): customWsException => {
return new customWsException(WS_BAD_REQUEST_ERROR, message);
};

export const WSDBUpdateFailureException = (
message?: string,
): customWsException => {
Expand Down
5 changes: 5 additions & 0 deletions src/common/exception/error-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export const WS_BAD_REQUEST_ERROR = new WsErrorCodeValueObject(
'WebSocket: bad request',
);

// 소켓 중복 로그인 에러
export const WS_DUPLICATE_LOGIN_ERROR = new WsErrorCodeValueObject(
'WebSocket: 새로 연결된 소켓이 있습니다.',
);

export const WS_DB_UPDATE_FAILURE = new WsErrorCodeValueObject(
'WebSocket: DataBase task could not be done',
);
66 changes: 35 additions & 31 deletions src/game/game.gateway.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import { InjectRedis } from '@liaoliaots/nestjs-redis';
import {
UseFilters,
UseGuards,
UsePipes,
ValidationPipe,
} from '@nestjs/common';
import {
ConnectedSocket,
MessageBody,
Expand All @@ -7,17 +14,18 @@ import {
WebSocketGateway,
WebSocketServer,
} from '@nestjs/websockets';
import { UsersRepository } from '../user-repository/users.repository';
import { FriendsRepository } from '../friends/friends.repository';
import Redis from 'ioredis';
import { Server, Socket } from 'socket.io';
import { ChannelsGateway } from '../channels/channels.gateway';
import { SocketWithAuth } from '../common/adapter/socket-io.adapter';
import { K } from '../common/constants';
import {
UseFilters,
UseGuards,
UsePipes,
ValidationPipe,
} from '@nestjs/common';
import { WsFilter } from '../common/exception/custom-ws-exception.filter';
import { GatewayCreateGameInvitationParamDto } from './dto/gateway-create-invitation-param.dto';
GameStatus,
GameType,
KEYNAME,
KEYSTATUS,
UserStatus,
} from '../common/enum';
import {
EVENT_ERROR,
EVENT_GAME_INVITATION,
Expand All @@ -30,31 +38,26 @@ import {
EVENT_SERVER_GAME_READY,
EVENT_USER_STATUS,
} from '../common/events';
import { ChannelsGateway } from '../channels/channels.gateway';
import { EmitEventInvitationReplyDto } from './dto/emit-event-invitation-reply.dto';
import { GameRepository } from './game.repository';
import { GameDto } from './dto/game.dto';
import {
GameStatus,
GameType,
KEYNAME,
KEYSTATUS,
UserStatus,
} from '../common/enum';
import { EmitEventMatchStatusDto } from './dto/emit-event-match-status.dto';
WSBadRequestException,
WSDuplicateLoginException,
} from '../common/exception/custom-exception';
import { WsFilter } from '../common/exception/custom-ws-exception.filter';
import { WsAuthGuard } from '../common/guards/ws-auth.guard';
import { FriendsRepository } from '../friends/friends.repository';
import { User } from '../user-repository/entities/user.entity';
import { UsersRepository } from '../user-repository/users.repository';
import { EmitEventInvitationReplyDto } from './dto/emit-event-invitation-reply.dto';
import { EmitEventMatchEndParamDto } from './dto/emit-event-match-end-param.dto';
import { EmitEventMatchScoreParamDto } from './dto/emit-event-match-score-param.dto';
import { EmitEventServerGameReadyParamDto } from './dto/emit-event-server-game-ready-param.dto';
import { EmitEventMatchStatusDto } from './dto/emit-event-match-status.dto';
import { EmitEventMatchmakingReplyDto } from './dto/emit-event-matchmaking-param.dto';
import { EmitEventServerGameReadyParamDto } from './dto/emit-event-server-game-ready-param.dto';
import { GameDto } from './dto/game.dto';
import { GatewayCreateGameInvitationParamDto } from './dto/gateway-create-invitation-param.dto';
import { UpdateGameResultParamDto } from './dto/update-game-result-param.dto';
import { InjectRedis } from '@liaoliaots/nestjs-redis';
import Redis from 'ioredis';
import { K } from '../common/constants';
import { UpdateDto } from './dto/view-map.dto';
import { User } from '../user-repository/entities/user.entity';
import { EmitEventMatchEndParamDto } from './dto/emit-event-match-end-param.dto';
import { WsAuthGuard } from '../common/guards/ws-auth.guard';
import { SocketWithAuth } from '../common/adapter/socket-io.adapter';
import { WSBadRequestException } from '../common/exception/custom-exception';
import { GameRepository } from './game.repository';

@WebSocketGateway({ namespace: 'game' })
@UseGuards(WsAuthGuard)
Expand Down Expand Up @@ -88,8 +91,9 @@ export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect {
const socket = this.userIdToClient.get(user.id);
if (socket) {
const result = await this.isSocketConnected(socket);
if (result) socket.disconnect();
else this.userIdToClient.delete(user.id);
if (result) {
throw WSDuplicateLoginException();
} else this.userIdToClient.delete(user.id);
} else {
await this.usersRepository.update(user.id, {
gameSocketId: null,
Expand Down
12 changes: 5 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import { SocketIoAdapter } from './common/adapter/socket-io.adapter';
import { WsFilter } from './common/exception/custom-ws-exception.filter';

async function bootstrap() {
const httpsOptions = {
key: fs.readFileSync('./BE-config/localhost-key.pem'),
cert: fs.readFileSync('./BE-config/localhost.pem'),
};
// const httpsOptions = {
// key: fs.readFileSync('./BE-config/localhost-key.pem'),
// cert: fs.readFileSync('./BE-config/localhost.pem'),
// };

const app = await NestFactory.create(AppModule, {
httpsOptions,
});
const app = await NestFactory.create(AppModule);

app.enableCors({
credentials: true,
Expand Down