This is an issue I have with websockets where connecting to a websocket hangs. The server is using a self-signed certificate, which has been already imported to the cacerts. Java code can make https requests without any issues regarding the certificate. When writing a client to listen updates from a websocket, the client never connects and hangs forever.
WebSocketClient client = new StandardWebSocketClient();
WebSocketStompClient stompClient = new WebSocketStompClient(client);
StompSessionHandler handler = new StompSessionHandler() {
@Override
public void handleFrame(StompHeaders headers, Object payload) {
System.err.println("handleFrame");
}
@Override
public Type getPayloadType(StompHeaders headers) {
return String.class;
}
@Override
public void handleTransportError(StompSession session, Throwable exception) {
System.err.println("handleTransportError");
}
@Override
public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
System.err.println("handleException");
}
@Override
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
System.err.println("afterConnected");
}
};
CompletableFuture<StompSession> connectAsync = stompClient.connectAsync("wss://localhost:5000/ws/", handler);
connectAsync.get();
System.err.println("Connected");
Thread.sleep(10000000);
I never see the print statement that say Connected or the print statements inside StompSessionHandler.
I am checking the server logs, and the server say that the handshake has been established and a connection is made. Furthermore I have enabled -Djavax.net.debug=all and I can see the updates and new messages from the websocket being printed in the logs.
javax.net.ssl|DEBUG|E3|WebSocketClient-SecureIO-2|2023-08-08 13:39:21.958 PDT|SSLEngineInputRecord.java:213|READ: TLSv1.2 application_data, length = 63
javax.net.ssl|DEBUG|E3|WebSocketClient-SecureIO-2|2023-08-08 13:39:21.958 PDT|SSLCipher.java:1675|Plaintext after DECRYPTION (
0000: 82 25 7B 22 74 6F 70 69 63 22 3A 22 73 79 73 74 .%."topic":"syst
0010: 65 6D 22 2C 22 68 62 22 3A 31 36 39 31 35 32 37 em","hb":1691527
0020: 31 36 31 38 33 30 7D 161830.
)
So Im guessing the client is in fact connecting but somehow facing issues that never confirms there is a connection.
Dependencies used:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.2</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
</dependency>
Update: Im changing the spring-boot version to 3.0.9 and I'm getting these logs:
14:08:57.642 [main] DEBUG org.springframework.web.socket.client.standard.StandardWebSocketClient -- Connecting to wss://localhost:5000/ws/
14:08:57.810 [SimpleAsyncTaskExecutor-1] DEBUG org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator -- New StandardWebSocketSession[id=c2fac7af-4d89-39a2-ccde-9e3c6d7e73ea, uri=null]
14:08:57.810 [SimpleAsyncTaskExecutor-1] DEBUG org.springframework.messaging.simp.stomp.DefaultStompSession -- Connection established in session id=ba753728-c5db-a6a1-8da8-8722d448745c
However, I'm still not seeing the print statements.
Comment From: wilkinsona
Thanks for the report. Unfortunately, without knowing more about the client and the server to which you're trying to connect, you haven't provided enough information for us to be able to help you. Given the level at which you're working, it's also more likely that, if there is a bug on the client-side, it's in Spring Framework or WebSocket client code, but of which are not maintained as part of Spring Boot.
If you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
Comment From: ek-ex
I can try to replicate the issue locally, however I dont own the server code. However I can confirm that the issue is not with the server as using other tools like Insomia or using Python to connect to the websocket and works just fine. Even further, using jakarta.websocket.WebSocketContainer works equally fine. I strongly believe that there is issue lies within org.springframework.web.socket.client.WebSocketClient implementation.
Comment From: wilkinsona
I strongly believe that there is issue lies within org.springframework.web.socket.client.WebSocketClient implementation.
Then you are in the wrong issue tracker. Spring Framework is managed as a separate project. If you'd like to pursue this, please gather as much information as possible about the problem and then open a Spring Framework issue.