Created
July 13, 2025 18:03
-
-
Save arleighdickerson/d861f1d1af75f6a438c9cb5bebeb07d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.arleigh.gantry.activeclient.ActiveClientService; | |
import io.arleigh.gantry.clientsession.ClientSessionService; | |
import io.arleigh.gantry.util.SecurityUtils; | |
import lombok.RequiredArgsConstructor; | |
import lombok.extern.slf4j.Slf4j; | |
import lombok.val; | |
import org.springframework.context.event.EventListener; | |
import org.springframework.messaging.simp.SimpMessagingTemplate; | |
import org.springframework.messaging.simp.user.SimpUser; | |
import org.springframework.messaging.simp.user.SimpUserRegistry; | |
import org.springframework.stereotype.Component; | |
import org.springframework.web.socket.messaging.SessionConnectedEvent; | |
import org.springframework.web.socket.messaging.SessionDisconnectEvent; | |
import java.util.Objects; | |
import java.util.Optional; | |
@Component | |
@Slf4j | |
@RequiredArgsConstructor | |
class ProviderWebSocketEvents { | |
private final SimpUserRegistry simpUserRegistry; | |
private final SimpMessagingTemplate simpMessagingTemplate; | |
private final ActiveClientService activeClientService; | |
private final ClientSessionService clientSessionService; | |
@EventListener(condition = "@securityUtils.isAuthenticated(#event.user)") | |
public void stompDidConnect(SessionConnectedEvent event) { | |
val eventUser = Objects.requireNonNull(event.getUser()); | |
val provider = SecurityUtils.getPrincipalName(eventUser); | |
val hasSessions = Optional.ofNullable(simpUserRegistry.getUser(provider)) | |
.map(SimpUser::hasSessions) | |
.orElse(false); | |
if (!hasSessions) { | |
return; | |
} | |
simpMessagingTemplate.convertAndSend( | |
"/topic/" + ProviderController.AVAILABILITY_PATH.replace(".", "." + provider + "."), | |
ProviderAvailability.BUSY | |
); | |
getActiveClientUsername(provider).ifPresent(username -> { | |
simpMessagingTemplate.convertAndSendToUser( | |
username, | |
"/queue/" + ProviderController.AVAILABILITY_PATH, | |
ProviderAvailability.AVAILABLE | |
); | |
}); | |
} | |
@EventListener(condition = "@securityUtils.isAuthenticated(#event.user)") | |
public void stompIsDisconnecting(SessionDisconnectEvent event) { | |
val eventUser = Objects.requireNonNull(event.getUser()); | |
val provider = SecurityUtils.getPrincipalName(eventUser); | |
val hasSessions = Optional.ofNullable(simpUserRegistry.getUser(provider)) | |
.map(SimpUser::hasSessions) | |
.orElse(false); | |
if (hasSessions) { | |
return; | |
} | |
getActiveClientUsername(provider).ifPresent(username -> { | |
simpMessagingTemplate.convertAndSendToUser( | |
username, | |
"/queue/" + ProviderController.AVAILABILITY_PATH, | |
ProviderAvailability.OFFLINE | |
); | |
}); | |
simpMessagingTemplate.convertAndSend( | |
"/topic/" + ProviderController.AVAILABILITY_PATH.replace(".", "." + provider + "."), | |
ProviderAvailability.OFFLINE | |
); | |
} | |
private Optional<String> getActiveClientUsername(String providerLogin) { | |
return Optional.ofNullable(activeClientService.getActiveClient(providerLogin)).flatMap(clientSessionService::getUsername); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment