diff --git a/src/main/java/com/chat/yourway/config/websocket/WebSocketConfig.java b/src/main/java/com/chat/yourway/config/websocket/WebSocketConfig.java index fb74aeee..c14d7a3e 100644 --- a/src/main/java/com/chat/yourway/config/websocket/WebSocketConfig.java +++ b/src/main/java/com/chat/yourway/config/websocket/WebSocketConfig.java @@ -6,6 +6,7 @@ import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; +import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration; @Configuration @EnableWebSocketMessageBroker @@ -25,4 +26,11 @@ public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint(properties.getEndpoint()); registry.addEndpoint(properties.getEndpoint()).setAllowedOriginPatterns("*").withSockJS(); } + + + @Override + public void configureWebSocketTransport(WebSocketTransportRegistration registry) { + registry.setTimeToFirstMessage(properties.getTimeToFirstMessage()); + } + } diff --git a/src/main/java/com/chat/yourway/config/websocket/WebsocketProperties.java b/src/main/java/com/chat/yourway/config/websocket/WebsocketProperties.java index 83e8eca4..8c000619 100644 --- a/src/main/java/com/chat/yourway/config/websocket/WebsocketProperties.java +++ b/src/main/java/com/chat/yourway/config/websocket/WebsocketProperties.java @@ -17,5 +17,6 @@ public class WebsocketProperties { private String topicPrefix; private String notifyPrefix; private String errorPrefix; + private int TimeToFirstMessage; } diff --git a/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java b/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java index 2fae796c..bf0766cc 100644 --- a/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java +++ b/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java @@ -1,7 +1,7 @@ package com.chat.yourway.listener; +import static com.chat.yourway.model.event.EventType.ONLINE; import static com.chat.yourway.model.event.EventType.SUBSCRIBED; -import static com.chat.yourway.model.event.EventType.UNSUBSCRIBED; import com.chat.yourway.config.websocket.WebsocketProperties; import com.chat.yourway.dto.response.LastMessageResponseDto; @@ -46,10 +46,11 @@ public void handleWebSocketSubscribeListener(SessionSubscribeEvent event) { .getLastMessage(); var contactEvent = new ContactEvent(email, getTopicId(event), SUBSCRIBED, getTimestamp(event), lastMessageDto); + contactEventService.updateEventTypeByEmail(ONLINE, email); contactEventService.save(contactEvent); } - chatNotificationService.notifyTopicSubscribers(getTopicId(event)); + chatNotificationService.notifyAllWhoSubscribedToSameUserTopic(email); chatNotificationService.notifyAllWhoSubscribedToTopic(getTopicId(event)); } catch (NumberFormatException e) { @@ -70,7 +71,7 @@ public void handleWebSocketUnsubscribeListener(SessionUnsubscribeEvent event) { try { if (isTopicDestination(destination)) { - var contactEvent = new ContactEvent(email, getTopicId(event), UNSUBSCRIBED, + var contactEvent = new ContactEvent(email, getTopicId(event), ONLINE, getTimestamp(event), lastMessageDto); contactEventService.save(contactEvent); } diff --git a/src/main/java/com/chat/yourway/model/event/EventType.java b/src/main/java/com/chat/yourway/model/event/EventType.java index 03916f44..f72005fe 100644 --- a/src/main/java/com/chat/yourway/model/event/EventType.java +++ b/src/main/java/com/chat/yourway/model/event/EventType.java @@ -3,6 +3,5 @@ public enum EventType { ONLINE, OFFLINE, - SUBSCRIBED, - UNSUBSCRIBED + SUBSCRIBED } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 023afd21..ecf8cbc5 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -16,7 +16,7 @@ spring.data.redis.password=${REDIS_PASSWORD:admin} #Hibernate spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect -spring.jpa.show-sql=true +#spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true spring.jpa.hibernate.ddl-auto=validate @@ -30,6 +30,7 @@ socket.endpoint=/chat socket.topic-prefix=/topic socket.notify-prefix=/specific/notify socket.error-prefix=/specific/error +socket.time-to-first-message=999999 #Security: security.jwt.token-type=Bearer @@ -58,5 +59,5 @@ spring.mail.properties.mail.smtp.starttls.required=true message.max.amount.reports=2 #Logging: -logging.level.com.chat.yourway=${LOGGING_LEVEL:info} +logging.level.com.chat.yourway=${LOGGING_LEVEL:trace} diff --git a/src/test/java/com/chat/yourway/unit/listener/StompSubscriptionListenerTest.java b/src/test/java/com/chat/yourway/unit/listener/StompSubscriptionListenerTest.java index 481ce221..38685b4a 100644 --- a/src/test/java/com/chat/yourway/unit/listener/StompSubscriptionListenerTest.java +++ b/src/test/java/com/chat/yourway/unit/listener/StompSubscriptionListenerTest.java @@ -1,7 +1,7 @@ package com.chat.yourway.unit.listener; +import static com.chat.yourway.model.event.EventType.ONLINE; import static com.chat.yourway.model.event.EventType.SUBSCRIBED; -import static com.chat.yourway.model.event.EventType.UNSUBSCRIBED; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.mockito.ArgumentMatchers.anyInt; @@ -108,7 +108,7 @@ public void handleWebSocketSubscribeListener_shouldNotifyTopicSubscribers() { stompSubscriptionListener.handleWebSocketSubscribeListener(event); // Then - verify(chatNotificationService, times(1)).notifyTopicSubscribers(topicId); + verify(chatNotificationService, times(1)).notifyAllWhoSubscribedToSameUserTopic(email); } @Test @@ -132,7 +132,7 @@ public void handleWebSocketUnsubscribeListener_shouldSaveEvent() { assertThat(capturedEvent.getTopicId()).isEqualTo(topicId); assertThat(capturedEvent.getEmail()).isEqualTo(email); assertThat(capturedEvent.getTimestamp()).isInstanceOfAny(LocalDateTime.class); - assertThat(capturedEvent.getEventType()).isEqualTo(UNSUBSCRIBED); + assertThat(capturedEvent.getEventType()).isEqualTo(ONLINE); } @Test