WebSocket connection failed: Error during WebSocket handshake: Unexpected response code: 400

后端 未结 16 1452
攒了一身酷
攒了一身酷 2020-12-04 21:08

I am trying to integrate Socket.io with Angular and I\'m having difficulties making a connection from the client-side to the server. I\'ve looked through other related quest

16条回答
  •  Happy的楠姐
    2020-12-04 21:37

    The problem for me was not got the port from process.env.PORT it is very important because Haruko and other services properly do a random port numbers to use.

    So that is the code that work for me eventuly :

    var app = require('express')();
    var http = require('http').createServer(app);
    const serverPort = process.env.PORT ; //<----- important 
    
    const io = require('socket.io')(http,{
      cors: {
        origin: '*',
        methods: 'GET,PUT,POST,DELETE,OPTIONS'.split(','),
        credentials: true
      }
    });
    
    http.listen(serverPort,()=>{
      console.log(`server listening on port ${serverPort}`)
    })
    

提交回复
热议问题