From 25fcd1021fc1846f3a9cf121e68c02bc4d9cfa08 Mon Sep 17 00:00:00 2001 From: AmirHossein Mahmoodi Date: Sun, 4 May 2025 12:12:24 +0330 Subject: [PATCH] fixed cors --- src/main.ts | 4 ++++ src/socket/socket.gateway.ts | 14 +++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main.ts b/src/main.ts index 07767c8..4931575 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,6 +5,10 @@ import { IoAdapter } from '@nestjs/platform-socket.io'; async function bootstrap() { const app = await NestFactory.create(AppModule); + app.enableCors({ + origin: '*', + credentials: true, + }); app.useGlobalPipes(new ValidationPipe()); app.useWebSocketAdapter(new IoAdapter(app)); await app.listen(parseInt(process.env.PORT)); diff --git a/src/socket/socket.gateway.ts b/src/socket/socket.gateway.ts index 4524eda..567ed6c 100644 --- a/src/socket/socket.gateway.ts +++ b/src/socket/socket.gateway.ts @@ -5,12 +5,20 @@ import { WebSocketServer, } from '@nestjs/websockets'; import { Server, Socket } from 'socket.io'; -import { SocketService } from './socket.service'; import { SendMessageDto } from './dto/sendMessage.dto'; +import { SocketService } from './socket.service'; -@WebSocketGateway() +@WebSocketGateway({ + namespace: '/notification', + cors: { + origin: '*', + }, + transports: ['websocket'], + path: '/notification/socket.io', // آدرس سفارشی +}) export class SocketGateway implements OnGatewayConnection { - @WebSocketServer() server: Server; + @WebSocketServer() + server: Server; constructor(private socketService: SocketService) {}