Skip to content

Commit c19280e

Browse files
committed
Fix error in WebSocketManager on empty user subscription
1 parent 7679e85 commit c19280e

File tree

5 files changed

+43
-22
lines changed

5 files changed

+43
-22
lines changed

crypto-messenger-coder/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>org.example</groupId>
99
<artifactId>crypto-messenger</artifactId>
10-
<version>1.1.0</version>
10+
<version>1.1.1</version>
1111
</parent>
1212

1313
<artifactId>crypto-messenger-coder</artifactId>

crypto-messenger-desktop/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>org.example</groupId>
99
<artifactId>crypto-messenger</artifactId>
10-
<version>1.1.0</version>
10+
<version>1.1.1</version>
1111
</parent>
1212

1313
<artifactId>crypto-messenger-desktop</artifactId>

crypto-messenger-desktop/src/main/java/cryptomessenger/desktop/infrastructure/client/websocket/WebSocketManager.java

+39-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package cryptomessenger.desktop.infrastructure.client.websocket;
22

33
import cryptomessenger.desktop.infrastructure.client.websocket.handler.MessageHandler;
4+
import jakarta.annotation.PostConstruct;
45
import lombok.RequiredArgsConstructor;
6+
import lombok.SneakyThrows;
57
import lombok.extern.slf4j.Slf4j;
68
import org.springframework.beans.factory.annotation.Value;
79
import org.springframework.boot.context.event.ApplicationReadyEvent;
@@ -18,6 +20,7 @@
1820

1921
import java.lang.reflect.Type;
2022
import java.nio.charset.StandardCharsets;
23+
import java.util.Optional;
2124
import java.util.Set;
2225
import java.util.function.Consumer;
2326

@@ -31,42 +34,60 @@ public class WebSocketManager {
3134

3235
private final Set<MessageHandler> messageHandlers;
3336

37+
private final WebSocketStompClient client = new WebSocketStompClient(new StandardWebSocketClient());
38+
private StompSession session = null;
39+
private Set<Subscription> subscriptions = emptySet();
40+
3441
@Value("${app.server-base-url}")
3542
private String serverBaseUrl;
3643

37-
private StompSession session;
38-
private Set<Subscription> subscriptions = emptySet();
39-
40-
@EventListener(ApplicationReadyEvent.class)
41-
private void connect() {
42-
var client = new WebSocketStompClient(new StandardWebSocketClient());
44+
@PostConstruct
45+
private void initialize() {
4346
client.setMessageConverter(new StringMessageConverter(StandardCharsets.UTF_8));
44-
client.connectAsync(getConnectionUrl(), new SessionHandler())
45-
.thenAccept(establishedSession -> {
46-
log.info("Connection established");
47-
session = establishedSession;
48-
refreshSubscriptions();
49-
});
5047
}
5148

52-
private String getConnectionUrl() {
53-
return serverBaseUrl.replaceFirst("http", "ws") + "/ws";
49+
@EventListener(ApplicationReadyEvent.class)
50+
private void connect() {
51+
var url = serverBaseUrl.replaceFirst("http", "ws") + "/ws";
52+
client.connectAsync(url, new SessionHandler(this::connect)).thenAccept(establishedSession -> {
53+
log.info("Session established");
54+
session = establishedSession;
55+
refreshSubscriptions();
56+
});
5457
}
5558

5659
public void refreshSubscriptions() {
5760
subscriptions.forEach(Subscription::unsubscribe);
58-
subscriptions = messageHandlers.stream().map(handler -> {
61+
subscriptions = messageHandlers.stream()
62+
.map(this::subscribe)
63+
.flatMap(Optional::stream)
64+
.collect(toSet());
65+
}
66+
67+
private Optional<Subscription> subscribe(MessageHandler handler) {
68+
var handlerName = handler.getClass().getSimpleName();
69+
try {
5970
var destination = handler.getDestination();
6071
var subscription = session.subscribe(destination, new FrameHandler(handler.getHandler()));
61-
log.info("Subscribed to {}", destination);
62-
return subscription;
63-
}).collect(toSet());
72+
log.info("Subscribed {} to {}", handlerName, destination);
73+
return Optional.of(subscription);
74+
} catch (Exception e) {
75+
log.warn("Failed to subscribe {}: {}", handlerName, e.getMessage());
76+
return Optional.empty();
77+
}
6478
}
6579

80+
@RequiredArgsConstructor
6681
private static class SessionHandler extends StompSessionHandlerAdapter {
82+
83+
private final Runnable onDisconnected;
84+
6785
@Override
86+
@SneakyThrows
6887
public void handleTransportError(StompSession session, Throwable exception) {
6988
log.warn("Transport error: {}", exception.getMessage());
89+
Thread.sleep(5000);
90+
onDisconnected.run();
7091
}
7192
}
7293

crypto-messenger-server/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>org.example</groupId>
99
<artifactId>crypto-messenger</artifactId>
10-
<version>1.1.0</version>
10+
<version>1.1.1</version>
1111
</parent>
1212

1313
<artifactId>crypto-messenger-server</artifactId>

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.example</groupId>
88
<artifactId>crypto-messenger</artifactId>
9-
<version>1.1.0</version>
9+
<version>1.1.1</version>
1010
<packaging>pom</packaging>
1111

1212
<modules>

0 commit comments

Comments
 (0)