Search in sources :

Example 1 with RoomMessage

use of com.github.chipolaris.bootforum.messaging.RoomMessage in project BootForum by chipolaris.

the class WebSocketEventsListener method handleSessionUnsubscribeEvent.

@EventListener
private void handleSessionUnsubscribeEvent(SessionUnsubscribeEvent event) {
    Message<byte[]> message = event.getMessage();
    // https://stackoverflow.com/questions/54330744/spring-boot-websocket-how-to-get-notified-on-client-subscriptions
    SimpMessageHeaderAccessor simpMessageHeaderAccessor = SimpMessageHeaderAccessor.wrap(message);
    String username = "Anonymous";
    Principal user = event.getUser();
    if (user != null) {
        username = user.getName();
    }
    String subscriptionId = simpMessageHeaderAccessor.getSubscriptionId();
    Map<String, Object> sessionAttributes = simpMessageHeaderAccessor.getSessionAttributes();
    String simpDestination = (String) sessionAttributes.get(subscriptionId);
    boolean removeResult = chatManager.removeSubscribedUser(simpDestination, username);
    if (removeResult) {
        logger.info(String.format("User %s unsubscribes from simpDestination: %s", username, simpDestination));
        this.template.convertAndSend(simpDestination, new RoomMessage(username, "leave", System.currentTimeMillis(), null));
    } else {
        logger.info(String.format("A session from user %s unsubscribes to simpDestination: %s", username, simpDestination));
    }
}
Also used : RoomMessage(com.github.chipolaris.bootforum.messaging.RoomMessage) Principal(java.security.Principal) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) EventListener(org.springframework.context.event.EventListener)

Example 2 with RoomMessage

use of com.github.chipolaris.bootforum.messaging.RoomMessage in project BootForum by chipolaris.

the class WebSocketEventsListener method handleSessionSubscribeEvent.

@EventListener
private void handleSessionSubscribeEvent(SessionSubscribeEvent event) {
    Message<byte[]> message = event.getMessage();
    // https://stackoverflow.com/questions/54330744/spring-boot-websocket-how-to-get-notified-on-client-subscriptions
    // String simpDestination = (String) message.getHeaders().get("simpDestination");
    // https://stackoverflow.com/questions/54658349/detect-destination-channel-of-sessionunsubscribeevent
    SimpMessageHeaderAccessor simpMessageHeaderAccessor = SimpMessageHeaderAccessor.wrap(message);
    String simpDestination = simpMessageHeaderAccessor.getDestination();
    String subscriptionId = simpMessageHeaderAccessor.getSubscriptionId();
    Map<String, Object> sessionAttributes = simpMessageHeaderAccessor.getSessionAttributes();
    String username = "Anonymous";
    Principal user = event.getUser();
    if (user != null) {
        username = user.getName();
    }
    boolean addResult = chatManager.addSubscribedUser(simpDestination, username);
    sessionAttributes.put(subscriptionId, simpDestination);
    if (addResult) {
        logger.info(String.format("User: %s subscribes to simpDestination: %s", username, simpDestination));
        Boolean avatarExists = fileHandler.isAvatarExists(username);
        // so the first message sent below have better chance to reach the newly established connection
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.template.convertAndSend(simpDestination, new RoomMessage(username, "enter", System.currentTimeMillis(), avatarExists));
    } else {
        logger.info(String.format("Another session from user: %s subscribes to simpDestination: %s", username, simpDestination));
    }
}
Also used : RoomMessage(com.github.chipolaris.bootforum.messaging.RoomMessage) Principal(java.security.Principal) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) EventListener(org.springframework.context.event.EventListener)

Example 3 with RoomMessage

use of com.github.chipolaris.bootforum.messaging.RoomMessage in project BootForum by chipolaris.

the class WebSocketEventsListener method handleSessionDisconnectEvent.

@EventListener
private void handleSessionDisconnectEvent(SessionDisconnectEvent event) {
    String username = "Anonymous";
    Principal user = event.getUser();
    if (user != null) {
        username = user.getName();
    }
    logger.info(String.format("Disconnect username: %s", username));
    chatManager.removeConnectedUser(username);
    this.template.convertAndSend("/system", new SystemMessage(String.format("User \"%s\" disconnected", username), System.currentTimeMillis()));
    // remove subscriptions to each chat room if exists
    Message<byte[]> message = event.getMessage();
    SimpMessageHeaderAccessor simpMessageHeaderAccessor = SimpMessageHeaderAccessor.wrap(message);
    Map<String, Object> sessionAttributes = simpMessageHeaderAccessor.getSessionAttributes();
    if (sessionAttributes.size() > 0) {
        for (String key : sessionAttributes.keySet()) {
            String simpDestination = (String) sessionAttributes.get(key);
            boolean removeResult = chatManager.removeSubscribedUser(simpDestination, username);
            if (removeResult) {
                logger.info(String.format("User %s unsubscribes from simpDestination: %s", username, simpDestination));
                this.template.convertAndSend(simpDestination, new RoomMessage(username, "leave", System.currentTimeMillis(), null));
            } else {
                logger.info(String.format("A session from user %s unsubscribes to simpDestination: %s", username, simpDestination));
            }
        }
    }
}
Also used : SystemMessage(com.github.chipolaris.bootforum.messaging.SystemMessage) RoomMessage(com.github.chipolaris.bootforum.messaging.RoomMessage) Principal(java.security.Principal) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) EventListener(org.springframework.context.event.EventListener)

Aggregations

RoomMessage (com.github.chipolaris.bootforum.messaging.RoomMessage)3 Principal (java.security.Principal)3 EventListener (org.springframework.context.event.EventListener)3 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)3 SystemMessage (com.github.chipolaris.bootforum.messaging.SystemMessage)1