0

I have got a WebSocket app which is developed in Spring maven framework which works really well when it is deployed in localhost:8080/test where test is my socket endpoint. Now I deployed the app in my AWS cloud server and tried to connect to the cloud server using the same method but using a specific URL (For example, https://example.com/test) where test is the WebSocket endpoint and URL(example.com) being my link similar to localhost:8080. I have also opened the port needed for the necessary connection in the security group.

For localhost it works fine but when I try to connect it to my cloud server always the WebSocket returns with an error function of status 400. I am not certain why a status 400 is coming because according to my knowledge status 400 means that it is a bad request. What am I doing wrong guys?

I am pasting my java code below

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/ws/");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/test").setAllowedOrigins("*"); // This will allow you to use ws://localhost:8080/test to establish websocket connection
        registry.addEndpoint("/test").setAllowedOrigins("*").withSockJS(); // This will allow you to use http://localhost:8080/test to establish websocket connection
    }

}

Is it any configuration that I am missing or any code snippet that I am missing?

P.S: All my testing is done mainly using POSTMAN Websocket request and YES!!! I use wss:// for example.com

My POSTMAN URL tends to be wss://example.com/test ------> Doesn't work

ws://localhost:8080/test -------> This works

Duderocks
  • 21
  • 6
  • Hi. Lets start with determining if request is reaching to your application server / webserver via logs. If logs are present, it means Spring is denying ther request for some reason, otherwise it is AWS configuration that is denying. PS. Please check if you have allowed TCP connection instead of just HTTP. – Gaurav Mar 02 '22 at 06:08
  • Are you enabling SSL in your websocket server? You are listening on port 8080 it seems. So the request is not handled by your web server. I don't know about Java but I had faced a similar issue while building a websocket server in Node. I had to manually provide the certificate and private keys to the server in order for it to listen on a secure channel. Also in that case you need to use wss:// in your client to connect to the websocket. – Yash Gadhiya Mar 02 '22 at 07:29
  • @Paras Yes, it is reaching the server. – Duderocks Mar 02 '22 at 16:09

1 Answers1

0

I found the issue. I have been hosting my tomcat server behind an Apache server which actually routes the traffic to the tomcat. So I was getting the error Spring WebSocket: Handshake failed due to invalid Upgrade header: null.

I have found the fix for this from Spring WebSocket: Handshake failed due to invalid Upgrade header: null

Duderocks
  • 21
  • 6