Home > OS >  Cannot connect to websocket when I change from localhost to a particular URL address
Cannot connect to websocket when I change from localhost to a particular URL address

Time:03-06

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

CodePudding user response:

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

  • Related