我想在nestjs后端配置管理界面与套接字,但它给cors错误,即使在添加cors的起源。请帮助什么是错误的或我失踪。
import { OnModuleInit } from '@nestjs/common';
import {
MessageBody,
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
OnGatewayInit
} from '@nestjs/websockets';
import { instrument } from '@socket.io/admin-ui';
import { Server } from 'socket.io';
@WebSocketGateway(undefined, {
cors: { origin: ['https://admin.socket.io'], credentials: true }
})
export class ChatGateway implements OnGatewayInit, OnModuleInit {
@WebSocketServer()
server: Server;
afterInit() {
instrument(this.server, {
auth: false
});
}
onModuleInit() {
this.server.on('connection', (socket) => {
console.log('client connected', socket.id);
socket.on('disconnect', (reason) => {
console.log(`disconnect ${socket.id} due to ${reason}`);
});
});
}
}
1条答案
按热度按时间nhjlsmyf1#
这对我来说很好用
外部代码之间唯一区别是我使用的是“开发”模式