From 987f2635b4dc67adfcb4c8c371a525d83714920c Mon Sep 17 00:00:00 2001 From: Kim KyeongGeun Date: Tue, 5 Dec 2023 18:08:03 +0900 Subject: [PATCH] =?UTF-8?q?[fix]=20WebSocket=20=EC=97=90=EB=9F=AC=20?= =?UTF-8?q?=EB=B0=98=ED=99=98=20=EC=8B=9C=20=EC=84=9C=EB=B2=84=20=EA=BA=BC?= =?UTF-8?q?=EC=A7=80=EB=8A=94=20=EB=AC=B8=EC=A0=9C=20=ED=95=B4=EA=B2=B0=20?= =?UTF-8?q?(#134)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: WebSocket 에러 반환 시 로그 출력 * feat: 로컬 테스트 환경 개선 --- docker-compose.yml | 2 ++ server/api-server/package.json | 2 +- server/api-server/src/chat/chat.gateway.ts | 16 +++++++--- server/api-server/src/main.ts | 34 ++++++++++++---------- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 5e5baf4..0ccd399 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -45,6 +45,8 @@ services: SESSION_SECRET: ${SESSION_SECRET} CLIENT_ORIGIN: ${CLIENT_ORIGIN} ENCODING_URL: rtmp://172.20.0.10/stream + PORT: 3000 + NODE_ENV: development rtmp-server: build: ./server/rtmp-server diff --git a/server/api-server/package.json b/server/api-server/package.json index 853e677..2acbe62 100644 --- a/server/api-server/package.json +++ b/server/api-server/package.json @@ -8,7 +8,7 @@ "scripts": { "build": "nest build", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", - "start": "NODE_ENV=production nest start", + "start": "nest start", "start:dev": "NODE_ENV=development nest start --watch", "start:debug": "nest start --debug --watch", "start:prod": "NODE_ENV=production node dist/main", diff --git a/server/api-server/src/chat/chat.gateway.ts b/server/api-server/src/chat/chat.gateway.ts index 2f16c50..66f41ad 100644 --- a/server/api-server/src/chat/chat.gateway.ts +++ b/server/api-server/src/chat/chat.gateway.ts @@ -11,7 +11,9 @@ import { Server, Socket } from 'socket.io'; import { JoinPayload } from './dto/join-payload.dto'; import { ChatPayload } from './dto/chat-payload'; import { Logger } from '@nestjs/common'; -import { UsersService } from 'src/users/users.service'; +import { User } from 'src/users/entities/user.entity'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; @WebSocketGateway({ cors: { @@ -20,15 +22,21 @@ import { UsersService } from 'src/users/users.service'; }, }) export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { - constructor(private readonly userService: UsersService) {} + constructor(@InjectRepository(User) private userRepo: Repository) {} @WebSocketServer() server: Server = new Server({ cookie: true }); private readonly logger = new Logger(ChatGateway.name); async handleConnection(client: Socket) { - const id = client.handshake['session'].userId || ''; + const userId = client.handshake['session'].userId || ''; + + let user: User; + try { + user = await this.userRepo.findOne({ where: { userId } }); + } catch (e) { + this.logger.error(e); + } - const user = await this.userService.findOne(id); client.data.userId = user?.userId || 'anonymous'; client.data.nickname = user?.nickname || '익명'; diff --git a/server/api-server/src/main.ts b/server/api-server/src/main.ts index f1f0292..51ef4ff 100644 --- a/server/api-server/src/main.ts +++ b/server/api-server/src/main.ts @@ -8,22 +8,24 @@ import * as http from 'http'; import * as express from 'express'; import { ExpressAdapter } from '@nestjs/platform-express'; import * as process from 'process'; +import { INestApplication } from '@nestjs/common'; async function bootstrap() { - const httpsOptions = { - key: fs.readFileSync(process.env.KEY_PATH), - cert: fs.readFileSync(process.env.CERT_PATH), - }; + let app: INestApplication; const server = express(); - const app = await NestFactory.create(AppModule, new ExpressAdapter(server), { - httpsOptions, - }); - app.enableCors({ - origin: process.env.CLIENT_ORIGIN, - methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS', - credentials: true, - }); + if (process.env.NODE_ENV === 'production') { + const httpsOptions = { + key: fs.readFileSync(process.env.KEY_PATH), + cert: fs.readFileSync(process.env.CERT_PATH), + }; + + app = await NestFactory.create(AppModule, new ExpressAdapter(server), { + httpsOptions, + }); + } else { + app = await NestFactory.create(AppModule); + } app.enableCors({ origin: process.env.CLIENT_ORIGIN, @@ -40,10 +42,12 @@ async function bootstrap() { await redisIoAdapter.connectToRedis(); app.useWebSocketAdapter(redisIoAdapter); - await app.init(); + if (process.env.NOD_ENV === 'production') { + await app.init(); + http.createServer(server).listen(3000); + } - app.listen(443); - http.createServer(server).listen(3000); + app.listen(process.env.PORT); } bootstrap();