Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The HTTP response from the server [200] did not permit the HTTP upgrade to WebSocket

I have my websocket client developed in STOMP Java client (Spring project) and server implemented in Spring boot.

When the client/server handshake happens, I am getting a connection upgrade issue.

Java Client Code

List<Transport> transports = new ArrayList<>(1);
transports.add(new WebSocketTransport(new StandardWebSocketClient()));      

SockJsClient sockjsClient = new SockJsClient(transports);   

WebSocketStompClient stompClient = new WebSocketStompClient(sockjsClient);

stompClient.setMessageConverter(new MappingJackson2MessageConverter());

stompClient.setTaskScheduler(new ConcurrentTaskScheduler());

StompSessionHandler sessionHandler = new SessionHandler();
stompClient.connect("ws://localhost:9090/health", sessionHandler);

Server side

@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {       
    stompEndpointRegistry.addEndpoint("/health")
            .setAllowedOrigins("*")
            .withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker("/topic");
    registry.setApplicationDestinationPrefixes("/app");
}

Exception generated at client side while connection to server

16:18:50.955/3771 [SimpleAsyncTaskExecutor-1] ERROR o.s.w.s.s.c.DefaultTransportRequest - No more fallback transports after TransportRequest[url=ws://localhost:9090/health/29/344d627baac949f5bab5506f05f1a7eb/websocket]
javax.websocket.DeploymentException: The HTTP response from the server [200] did not permit the HTTP upgrade to WebSocket
               at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServerRecursive(WsWebSocketContainer.java:434)
               at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServerRecursive(WsWebSocketContainer.java:392)
               at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:194)
               at org.springframework.web.socket.client.standard.StandardWebSocketClient.lambda$doHandshakeInternal$0(StandardWebSocketClient.java:150)
               at java.util.concurrent.FutureTask.run(FutureTask.java:266)
               at java.lang.Thread.run(Thread.java:745)

In the tomcat localhost_access_log I see the below request info

127.0.0.1 - - [01/Nov/2018:22:09:41 +0530] "GET /health/info HTTP/1.1" 302 - 0
127.0.0.1 - - [01/Nov/2018:22:09:41 +0530] "GET /login.jsp HTTP/1.1" 200 7649 16
127.0.0.1 - - [01/Nov/2018:22:09:41 +0530] "GET /health/191/6828a1fdefee40cf8dc74e825d8d2b0c/websocket HTTP/1.1" 302 - 0
127.0.0.1 - - [01/Nov/2018:22:09:41 +0530] "GET /login.jsp HTTP/1.1" 200 7649 0

I didn't find any info on how to fix this issue and what is causing it.

Please help me resolve this issue.

like image 567
Mohan Avatar asked Nov 01 '18 16:11

Mohan


1 Answers

Too late to be able to present a helpful resolution for your problem. Today I'm facing the same issue that you have already experienced. Unfortunately, Spring Framework samples and official resources are not referencing entirely. Therefore, I would like to write about how I made it up and running. Firstly, the original answer doesn't belong to me. I found it at https://code5.cn/so/java/783097.

As far as I can understand, to be able to upgrade the WebSocket protocol from HTTP, we should extend the "WebSocketConfiguration" part a little bit with desired capabilities.

RequestUpgradeStrategy upgradeStrategy = new TomcatRequestUpgradeStrategy();
registry.addEndpoint("/hello")
        .withSockJS();

registry.addEndpoint("/hello")
        .setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy))
        .setAllowedOrigins("*");

After making the above adjustments, you could comprehend regarding switching between protocols through sniffing WireShark.

enter image description here

I hope it would be helpful for someone who is looking for a way to get rid of errors.

like image 127
nurisezgin Avatar answered Sep 28 '22 21:09

nurisezgin