[英]Java Glassfish Spring Sockjs failed: Error during WebSocket handshake: Unexpected response code: 500
[英]Spring, SockJS - error during WebSocket handshake: Unexpected response code 400
我已经使用 Spring Boot 实现了具有 WebSockets 支持的 BE 应用程序。 我使用 SockJS 来连接到我的 WebSocket 端点,在连接过程中我收到以下错误:
error during WebSocket handshake: Unexpected response code: 400
但是随后(如您在上图中所见)一切正常并且 websocket 已打开。
现在我不明白这个问题的原因是什么以及如何解决它。 请帮忙。
更新
感谢paweln1986帮助我修复了 SockJS lib 的版本问题,但意外响应代码 400 的问题仍然存在:
我也在 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 版本。
在浏览器端,应用程序可以使用模拟 W3C WebSocket API 的 sockjs-client(版本 1.0.x)
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html
您在/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/
如果您将 SSL 与 Amazon 负载均衡器一起使用,也会出现这种复杂情况。 elb 的侦听器应该有一个 SSL(而不是 HTTPS)条目,负载均衡器端口为 443,实例协议为 TCP,实例端口为 80(不要忘记添加证书)这将使 WebSocket 流量通过。
详细参考: https : //blog.jverkamp.com/2015/07/20/configuring-websockets-behind-an-aws-elb/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.