java - Spring,SockJS - WebSocket 握手期间出错 : Unexpected response code 400

标签 java spring nginx websocket sockjs

我已经使用 Spring Boot 实现了支持 WebSockets 的 BE 应用程序。我使用 SockJS 连接到我的 WebSocket 端点,在连接过程中我收到以下错误:

error during WebSocket handshake: Unexpected response code: 400

enter image description here

但是随后(如上图所示)一切正常,websocket 打开了。

现在我不明白这个问题的原因是什么以及如何解决它。请帮忙。

已更新

感谢 paweln1986帮助我修复了 SockJS lib 的版本问题,但意外响应代码 400 的问题仍然存在:

enter image description here

我还在 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/;
    }        

} 

最佳答案

你的 SockJS 版本有误。

On the browser side, applications can use the sockjs-client (version 1.0.x) that emulates the W3C WebSocket API

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

您在 /api/ 部分下遗漏了两件事 proxy_set_header 升级 $http_upgrade; proxy_set_header 连接“升级”; https://www.nginx.com/blog/websocket-nginx/

关于java - Spring,SockJS - WebSocket 握手期间出错 : Unexpected response code 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40367068/

相关文章:

Java 将 8 位字符串二进制表示形式转换为字节并再次转换回来

java - 防止覆盖 application.properties 中的某些属性 - Spring Boot

java - 如何防止嵌入式netty服务器从spring-boot-starter-webflux启动?

spring - 在 Spring WebFlow 中,如何恢复流程?

django - 如何在Kubernetes中实际设置django静态文件的样式?

debugging - nginx php-fpm xdebug netbeans 只能启动一个调试 session

java - Selenium 2 : finding web element locators programmatically

java - 试图将java对象存储在连续内存中

java - 为什么 application.yml 中 user.name 属性的值是错误的?

django - 如何在 Docker Compose 中将目录从一个容器复制到另一个容器?