|
| 1 | +package cryptomessenger.desktop.infrastructure.client.websocket; |
| 2 | + |
| 3 | +import cryptomessenger.desktop.infrastructure.client.websocket.handler.MessageHandler; |
| 4 | +import lombok.RequiredArgsConstructor; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import org.springframework.beans.factory.annotation.Value; |
| 7 | +import org.springframework.boot.context.event.ApplicationReadyEvent; |
| 8 | +import org.springframework.context.event.EventListener; |
| 9 | +import org.springframework.messaging.converter.StringMessageConverter; |
| 10 | +import org.springframework.messaging.simp.stomp.StompFrameHandler; |
| 11 | +import org.springframework.messaging.simp.stomp.StompHeaders; |
| 12 | +import org.springframework.messaging.simp.stomp.StompSession; |
| 13 | +import org.springframework.messaging.simp.stomp.StompSession.Subscription; |
| 14 | +import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter; |
| 15 | +import org.springframework.stereotype.Component; |
| 16 | +import org.springframework.web.socket.client.standard.StandardWebSocketClient; |
| 17 | +import org.springframework.web.socket.messaging.WebSocketStompClient; |
| 18 | + |
| 19 | +import java.lang.reflect.Type; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.function.Consumer; |
| 23 | + |
| 24 | +import static java.util.Collections.emptySet; |
| 25 | +import static java.util.stream.Collectors.toSet; |
| 26 | + |
| 27 | +@Slf4j |
| 28 | +@Component |
| 29 | +@RequiredArgsConstructor |
| 30 | +public class WebSocketManager { |
| 31 | + |
| 32 | + private final Set<MessageHandler> messageHandlers; |
| 33 | + |
| 34 | + @Value("${app.server-base-url}") |
| 35 | + private String serverBaseUrl; |
| 36 | + |
| 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()); |
| 43 | + 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 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + private String getConnectionUrl() { |
| 53 | + return serverBaseUrl.replaceFirst("http", "ws") + "/ws"; |
| 54 | + } |
| 55 | + |
| 56 | + public void refreshSubscriptions() { |
| 57 | + subscriptions.forEach(Subscription::unsubscribe); |
| 58 | + subscriptions = messageHandlers.stream().map(handler -> { |
| 59 | + var destination = handler.getDestination(); |
| 60 | + var subscription = session.subscribe(destination, new FrameHandler(handler.getHandler())); |
| 61 | + log.info("Subscribed to {}", destination); |
| 62 | + return subscription; |
| 63 | + }).collect(toSet()); |
| 64 | + } |
| 65 | + |
| 66 | + private static class SessionHandler extends StompSessionHandlerAdapter { |
| 67 | + @Override |
| 68 | + public void handleTransportError(StompSession session, Throwable exception) { |
| 69 | + log.warn("Transport error: {}", exception.getMessage()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @RequiredArgsConstructor |
| 74 | + private static class FrameHandler implements StompFrameHandler { |
| 75 | + |
| 76 | + private final Consumer<String> consumer; |
| 77 | + |
| 78 | + @Override |
| 79 | + public Type getPayloadType(StompHeaders headers) { |
| 80 | + return String.class; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void handleFrame(StompHeaders headers, Object payload) { |
| 85 | + consumer.accept((String) payload); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments