简体   繁体   English

Spring,SockJS - WebSocket 握手期间出错:意外响应代码 400

[英]Spring, SockJS - error during WebSocket handshake: Unexpected response code 400

I have implemented BE application with WebSockets support using Spring Boot.我已经使用 Spring Boot 实现了具有 WebSockets 支持的 BE 应用程序。 I use SockJS in order to connect to my WebSocket endpoint and during the connection process I get a following error:我使用 SockJS 来连接到我的 WebSocket 端点,在连接过程中我收到以下错误:

error during WebSocket handshake: Unexpected response code: 400

在此处输入图片说明

but then (as you can see at the image above) everything is working fine and websocket opened.但是随后(如您在上图中所见)一切正常并且 websocket 已打开。

Right now I don't understand what can be a reason of this issue and how to fix it.现在我不明白这个问题的原因是什么以及如何解决它。 Please help.请帮忙。

UPDATED更新

Thanks for the paweln1986 help I have fixed version issue with SockJS lib but the issue with Unexpected response code 400 still exists:感谢paweln1986帮助我修复了 SockJS lib 的版本问题,但意外响应代码 400 的问题仍然存在:

在此处输入图片说明

I also using nginx in front of Tomcat 8. This is my nginx config:我也在 Tomcat 8 前面使用了 nginx。这是我的 nginx 配置:

server {
    listen          443 ssl;        
    server_name  myserver.com;
    ssl on;

    location /api/ {                                                                                                                                                                                                                                             
        proxy_pass_header X-XSRF-TOKEN;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
        proxy_pass        http://localhost:8081/api/;                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
        proxy_set_header Origin "http://localhost:8081/api/";                                                                                                                                                                                                    

        proxy_set_header Host $host;                                                                                                                                                                                                                             
        proxy_set_header X-Real-IP $remote_addr;                                                                                                                                                                                                                 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                                                                                                                                                                                             
        proxy_http_version 1.1;                                                                                                                                                                                                                                  
    }                                                                                                                                                                                                                                                            

    location /dashboard/ {
        proxy_pass        http://localhost:8081/dashboard/;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /manager {
        proxy_pass        http://localhost:8081/manager/;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }        

    location / {
        root   /srv/www/htdocs/myserver/;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /srv/www/htdocs/;
    }        

} 

You have wrong version of SockJS.你有错误的 SockJS 版本。

On the browser side, applications can use the sockjs-client (version 1.0.x) that emulates the W3C WebSocket API在浏览器端,应用程序可以使用模拟 W3C WebSocket API 的 sockjs-client(版本 1.0.x)

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html

You are missing two things under your /api/ section proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade";您在/api/部分中缺少两件事proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; https://www.nginx.com/blog/websocket-nginx/ https://www.nginx.com/blog/websocket-nginx/

This complication also occurs if you are using SSL with Amazon load balancers.如果您将 SSL 与 Amazon 负载均衡器一起使用,也会出现这种复杂情况。 The listener of the elb should have an entry of SSL(instead of HTTPS) with load balancer port as 443 and instance protocol as TCP with instance port as 80 (Don't forget to add certificate) This will enable WebSocket traffic to pass through. elb 的侦听器应该有一个 SSL(而不是 HTTPS)条目,负载均衡器端口为 443,实例协议为 TCP,实例端口为 80(不要忘记添加证书)这将使 WebSocket 流量通过。

For detailed reference : https://blog.jverkamp.com/2015/07/20/configuring-websockets-behind-an-aws-elb/详细参考: https : //blog.jverkamp.com/2015/07/20/configuring-websockets-behind-an-aws-elb/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Glassfish Spring Sockjs失败:WebSocket握手期间出错:意外的响应代码:500 - Java Glassfish Spring Sockjs failed: Error during WebSocket handshake: Unexpected response code: 500 WebSocket握手期间发生错误:意外的响应代码:带CoinIMP密钥的400 - Error during WebSocket handshake: Unexpected response code: 400 with CoinIMP keys WebSocket握手期间出错(意外的响应代码:400) - Error during WebSocket handshake (Unexpected response code: 400) WebSocket 握手期间出错:意外响应代码:400 Spring 启动 websockets - Error during WebSocket handshake: Unexpected response code: 400 Spring boot websockets 失败:WebSocket握手期间出错:意外的响应代码:404 - failed: Error during WebSocket handshake: Unexpected response code: 404 WebSocket握手期间出错:意外的响应代码:404 - Error during WebSocket handshake: Unexpected response code: 404 WebSocket握手期间出错:意外的响应代码:302 - Error during WebSocket handshake: Unexpected response code: 302 失败:WebSocket 握手期间出错:意外响应代码:302 - failed: Error during WebSocket handshake: Unexpected response code: 302 WebSocket 握手期间出错:意外响应代码:403 - Error during WebSocket handshake: Unexpected response code: 403 Spring 启动 WebSocket 连接到“ws://127.0.0.1:8081/getUsersList/”失败:WebSocket 握手期间出错:意外响应代码:200 - Spring boot WebSocket connection to 'ws://127.0.0.1:8081/getUsersList/' failed: Error during WebSocket handshake: Unexpected response code: 200
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM