*클라이언트 코드
npm package 설치
npm i --save @nestjs/websockets @nestjs/platform-socket.io socket.io
gateway.ts 전체 코드
@WebSocketGateway(+process.env.SOCKET_PORT, {
cors: true,
namespace: 'TicketNest-socket',
})
import { Server } from 'socket.io';
export class BookingGateway {
@WebSocketServer()
server: Server;
passOrderCountToQueue(userId: string, order: number) {
console.log('userId:', userId, 'order:', order);
this.server
// .to(userId)
.to('common-room') // common-room 이란 룸에 메세지 보냄. 해당 room에 있는 유저들에게만 브로드 캐스팅 됨.
.emit('waiting', `${userId}님의 대기 순서는 ${order}번 입니다`);
}
@SubscribeMessage('TicketNest-socket')
async passOrderToClient(client: Socket, userId: string) {
client.join('common-room');
}
}