Closed wujianyong1994 closed 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/
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
how is configured your app.module file?
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 {}
Please show me main.ts code
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); }
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();
Please, use StackOverflow for such questions
@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.
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.
https is finished
websocket:
web
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!!