websocket 如何连接socket admin UI和nestjs后端

mcvgt66p  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(103)

我想在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}`);
      });
    });
  }
}

nhjlsmyf

nhjlsmyf1#

这对我来说很好用

@WebSocketGateway(5002, {
  cors: {
    origin: ['https://admin.socket.io', 'http://localhost:3000'],
    credentials: true
  },
})

外部代码之间唯一区别是我使用的是“开发”模式

afterInit() {
      instrument(this.server, {
        auth: false,
        mode: "development",   
      });
  }

相关问题