Search in sources :

Example 1 with SystemMessage

use of com.github.chipolaris.bootforum.messaging.SystemMessage 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)

Example 2 with SystemMessage

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

the class WebSocketEventsListener method handleSessionConnectedEvent.

@EventListener
private void handleSessionConnectedEvent(SessionConnectedEvent event) {
    String username = "Anonymous";
    Principal user = event.getUser();
    if (user != null) {
        username = user.getName();
    }
    logger.info(String.format("Connect username: %s", username));
    chatManager.addConnectedUser(username);
    this.template.convertAndSend("/system", new SystemMessage(String.format("User \"%s\" connected", username), System.currentTimeMillis()));
}
Also used : SystemMessage(com.github.chipolaris.bootforum.messaging.SystemMessage) Principal(java.security.Principal) EventListener(org.springframework.context.event.EventListener)

Aggregations

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