10. Nest.js 광부왕 게임 서버 Socket.IO 설정
2023. 3. 4. 01:35ㆍ프로젝트/게임 서버 - Nest.js
1. @nestjs/platform-socket.io, socket.io 설치
npm i @nestjs/platform-socket.io socket.io
2.adapters/adapters.ts 생성
Nest 서버에서 socket.io를 사용하기 위해 생성한다.
adapters.ts 생성
import { IoAdapter } from '@nestjs/platform-socket.io';
export class SocketIoAdapter extends IoAdapter {
createIOServer(port: number, options?: any): any {
const server = super.createIOServer(port, options);
return server;
}
}
main.ts
( socket.io 설정 )
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import expressBasicAuth from 'express-basic-auth';
import { AppModule } from './app.module';
import { ErrorExceptionFilter } from './common/filters/error.exception';
import { SuccessInterceptor } from './common/interceptors/success.interceptor';
//추가
import { SocketIoAdapter } from './adapters/adapters';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalFilters(new ErrorExceptionFilter());
app.useGlobalInterceptors(new SuccessInterceptor());
//추가
app.useWebSocketAdapter(new SocketIoAdapter(app));
if (process.env.NODE_ENV === 'product') {
app.use(
['/docs'],
expressBasicAuth({
challenge: true,
users: { [process.env.SWAGGER_USER]: process.env.SWAGGER_PWD },
}),
);
}
const config = new DocumentBuilder()
.addBearerAuth()
.setTitle('광부왕')
.setDescription('king-of-mine-worker-back Api')
.setVersion(process.env.VERSION)
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('docs', app, document);
await app.listen(process.env.PORT);
}
bootstrap();
728x90
반응형
'프로젝트 > 게임 서버 - Nest.js' 카테고리의 다른 글
11. Nest.js 광부왕 게임 서버 Log 저장 모듈 생성 (0) | 2023.03.13 |
---|---|
9. Nest.js 광부왕 게임 서버 Decorator 생성 (0) | 2023.03.03 |
8. Nest.js 광부왕 게임 서버 JWT 설정 (0) | 2023.03.03 |
7. Nest.js 광부왕 게임 서버 Interceptor 적용 (0) | 2023.03.03 |
6. Nest.js 광부왕 게임 서버 Filter 적용 (0) | 2023.03.03 |