Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have implemented BE application with WebSockets support using Spring Boot. I use SockJS in order to connect to my WebSocket endpoint and during the connection process I get a following error:

error during WebSocket handshake: Unexpected response code: 400

enter image description here

but then (as you can see at the image above) everything is working fine and websocket opened.

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:

enter image description here

I also using nginx in front of Tomcat 8. This is my nginx config:

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/;
    }        

} 
like image 252
alexanoid Avatar asked Apr 14 '26 20:04

alexanoid


1 Answers

You have wrong version of 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

You are missing two things under your /api/ section proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; https://www.nginx.com/blog/websocket-nginx/

like image 107
PawelN Avatar answered Apr 16 '26 09:04

PawelN