3

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

} 
0

2 Answers 2

5

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/

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, where it should be fixed at client or server side ?
Thanks for your help! I have fixed version issue with SockJS lib but the issue with Unexpected response code 400 still exists:
are you using any reverse proxy (nginx)?
could you show your nginx configuration? I had similar issue caused by wrong nginx configuration
|
2

This complication also occurs if you are using SSL with Amazon load balancers. 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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.