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

后端 未结 16 1444
攒了一身酷
攒了一身酷 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条回答
  • 2020-12-04 21:40

    Judging from the messages you send via Socket.IO socket.emit('greet', { hello: 'Hey, Mr.Client!' });, it seems that you are using the hackathon-starter boilerplate. If so, the issue might be that express-status-monitor module is creating its own socket.io instance, as per: https://github.com/RafalWilinski/express-status-monitor#using-module-with-socketio-in-project

    You can either:

    1. Remove that module
    2. Pass in your socket.io instance and port as websocket when you create the expressStatusMonitor instance like below:

      const server = require('http').Server(app);
      const io = require('socket.io')(server);
      ...
      app.use(expressStatusMonitor({ websocket: io, port: app.get('port') })); 
      
    0 讨论(0)
  • 2020-12-04 21:42

    I think you should define your origins for client side as bellow:

    //server.js
    const socket = require('socket.io');
    const app = require('express')();
    const server = app.listen('port');
    
    const io = socket().attach(server);
    io.origins("your_domain:port www.your_domain:port your_IP:port your_domain:*")
    
    io.on('connection', (socket) => {
      console.log('connected a new client');
    });
    
    //client.js
    var socket = io('ws://:port');

    0 讨论(0)
  • 2020-12-04 21:47

    In my case, I have just install express-status-monitor to get rid of this error

    here are the settings

    install express-status-monitor
    npm i express-status-monitor --save
    const expressStatusMonitor = require('express-status-monitor');
    app.use(expressStatusMonitor({
        websocket: io,
        port: app.get('port')
    }));
    
    0 讨论(0)
  • 2020-12-04 21:48

    This worked for me with Nginx, Node server and Angular 4

    Edit your nginx web server config file as:

    server {
    listen 80;
    server_name 52.xx.xxx.xx;
    
    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         "http://127.0.0.1:4200";
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection "upgrade";
    }
    
    0 讨论(0)
  • 2020-12-04 21:50

    I solved this by changing transports from 'websocket' to 'polling'

       var socket = io.connect('xxx.xxx.xxx.xxx:8000', {
          transports: ['polling']
       });
    
    0 讨论(0)
  • 2020-12-04 21:50

    After using following load balancer setting my problem solved for wss but for ws problem still exists for specific one ISP.

    calssic-load-balancer

    0 讨论(0)
提交回复
热议问题