nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.89k stars 7.56k forks source link

nest.js https+websocket #1205

Closed wujianyong1994 closed 5 years ago

wujianyong1994 commented 5 years ago

https is finished

websocket:


import { SubscribeMessage, WebSocketGateway, WsResponse, WebSocketServer } from '@nestjs/websockets';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
@WebSocketGateway({path : '/room'})
export class EventsGateway {
  @WebSocketServer() server;
@SubscribeMessage('chat')
  chat(client: any, data: any) {
    this.server.emit('chat', data);
  }

}

web


import io from 'socket.io-client';
const socket = io(websocketUrl, {path : '/room'});
socket.emit('chat', {msg});

description

i used http+websocket ,it can work. But when i used https+websocket,it can't work. when i request the nest.js server with socket+https ,the browser get error: Failed to load https://t*********.club:3001/room/?EIO=3&transport=polling&t=MQ6GnWx: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:3000' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

response:{"statusCode":404,"error":"Not Found","message":"Cannot GET /room"}

How to solve , thx!!

karimsqualli commented 5 years ago

When creating the websocketGateway you need to specify the origin parameter,

By default the value of origin is *, it mean that it allow requeest from any host, in https, you need to restrist that to only the adress of your client. for example if you are working locally : localhost:3000

@WebSocketGateway({path : '/room', origin: 'localhost:3000'})

More information here: https://socket.io/docs/server-api/

wujianyong1994 commented 5 years ago

When creating the websocketGateway you need to specify the origin parameter,

By default the value of origin is *, it mean that it allow requeest from any host, in https, you need to restrist that to only the adress of your client. for example if you are working locally : localhost:3000

@WebSocketGateway({path : '/room', origin: 'localhost:3000'})

More information here: https://socket.io/docs/server-api/ thank you very much!!! i set the option of WebSocketGateway : @WebSocketGateway({path : '/room', origin: 'localhost:3000'}); But the same problem happened. Maybe i just use http+websocket

Root-Control commented 5 years ago

how is configured your app.module file?

wujianyong1994 commented 5 years ago

how is configured your app.module file?

import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { EventsGateway } from './app.gateway'

@Module({ imports: [], controllers: [AppController], providers: [AppService, EventsGateway] }) export class AppModule {}

Root-Control commented 5 years ago

Please show me main.ts code

wujianyong1994 commented 5 years ago

Please show me main.ts code ==========main.ts===== async function bootstrap() { console.log('start nestjs'); await Db.dbInit(config.db); if (process.env.NODE_ENV === 'production'){ init.initHttps(); } else { await init.init(); } } bootstrap(); =========init.initHttps()====== export async function initHttps(){ const httpsOptions = { key: fs.readFileSync('../Nginx/2_thebestguy.club.key'), cert: fs.readFileSync('../Nginx/1_thebestguy.club_bundle.crt') }; const server = express(); const app = await NestFactory.create(AppModule, server); app.enableCors(); await app.init(); https.createServer(httpsOptions, server).listen(3001); }

Root-Control commented 5 years ago

In my nestjs config i had app.useWebSocketAdapter(new WsAdapter(app.getHttpServer()))

I deleted the code and had no more problems, I think the problem is no longer your connection to the socket, the problem lies in the configuration of mainjs or maybe the express server, verify the configuration and make the tests.

Try running the nestjs server with basic configuration.

async function bootstrap() { const app = await NestFactory.create(ApplicationModule); await app.listen(3001); } bootstrap();

kamilmysliwiec commented 5 years ago

Please, use StackOverflow for such questions

1166544 commented 5 years ago

@karimsqualli Why not review the official website documents without providing corresponding help? I believe that most people tend to publish authority rather than quality assurance.

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.