Skip to content

Commit

Permalink
[fix] WebSocket 에러 반환 시 서버 꺼지는 문제 해결 (#134)
Browse files Browse the repository at this point in the history
* fix: WebSocket 에러 반환 시 로그 출력

* feat: 로컬 테스트 환경 개선
  • Loading branch information
kkg5 authored Dec 5, 2023
1 parent 073d259 commit 987f263
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server/api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 12 additions & 4 deletions server/api-server/src/chat/chat.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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<User>) {}

@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 || '익명';

Expand Down
34 changes: 19 additions & 15 deletions server/api-server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>;
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,
Expand All @@ -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();

0 comments on commit 987f263

Please sign in to comment.