ระดับกลาง
19 สิงหาคม 2569หาบั๊กให้เจอ: Node.js / NestJS
โค้ดหนึ่งชิ้น หาบั๊กให้เจอ หรือพิสูจน์ว่าไม่มีบั๊ก
typescript
1import { WebSocketGateway, SubscribeMessage, MessageBody, ConnectedSocket } from '@nestjs/websockets';
2import { Socket } from 'socket.io';
3import { MessagesService } from './messages.service';
4
5@WebSocketGateway({ namespace: 'chat' })
6export class ChatGateway {
7 constructor(private readonly messages: MessagesService) {}
8
9 @SubscribeMessage('joinRoom')
10 async joinRoom(
11 @ConnectedSocket() client: Socket,
12 @MessageBody() data: { roomId: string },
13 ) {
14 client.join(data.roomId);
15 const history = await this.messages.getRoomHistory(data.roomId);
16 client.emit('history', history);
17 }
18}คลิกบรรทัดที่เป็นต้นเหตุของปัญหา
คำตัดสินของคุณ
วันละหนึ่งครั้ง คำตอบจะแสดงทันทีหลังจากนั้น
รักษาสถิติต่อเนื่องของคุณ
สร้างบัญชีเพื่อเก็บสถิติต่อเนื่องและติดตามความคืบหน้า