251123:0200 T6.1 to DO
This commit is contained in:
38
backend/src/modules/notification/notification.gateway.ts
Normal file
38
backend/src/modules/notification/notification.gateway.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import {
|
||||
WebSocketGateway,
|
||||
WebSocketServer,
|
||||
OnGatewayConnection,
|
||||
OnGatewayDisconnect,
|
||||
} from '@nestjs/websockets';
|
||||
import { Server, Socket } from 'socket.io';
|
||||
import { Logger } from '@nestjs/common';
|
||||
|
||||
@WebSocketGateway({
|
||||
cors: {
|
||||
origin: '*',
|
||||
},
|
||||
namespace: 'notifications',
|
||||
})
|
||||
export class NotificationGateway
|
||||
implements OnGatewayConnection, OnGatewayDisconnect
|
||||
{
|
||||
@WebSocketServer()
|
||||
server!: Server; // ✅ FIX: เติม ! (Definite Assignment Assertion)
|
||||
|
||||
private readonly logger = new Logger(NotificationGateway.name);
|
||||
|
||||
handleConnection(client: Socket) {
|
||||
this.logger.log(`Client connected: ${client.id}`);
|
||||
}
|
||||
|
||||
handleDisconnect(client: Socket) {
|
||||
this.logger.log(`Client disconnected: ${client.id}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* ส่งแจ้งเตือนไปหา User แบบ Real-time
|
||||
*/
|
||||
sendToUser(userId: number, payload: any) {
|
||||
this.server.to(`user_${userId}`).emit('new_notification', payload);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user