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
반응형