Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get native header when a user connects

I have clients connecting like this on a websocket

var socket = new SockJS(this.props.stomp);
var stompClient = Stomp.over(socket);
var channel = "/room."+this.props.room.uid
var app = this;
stompClient.connect({room: this.props.room.uid}, function(frame) {
....

and on my Java backend I'm trying to get this header following a tutorial:

public void onApplicationEvent(SessionConnectEvent event) {
    StompHeaderAccessor sha = StompHeaderAccessor.wrap(event.getMessage());

    String  room = sha.getNativeHeader("room").get(0);
    logger.debug("Connect event [sessionId: " + sha.getSessionId() +"; room: "+ room + " ]");
}

but the room is null at this point.
I debugged and found a very complex hierarchy: a MessageHeaderAccessor contains a map that contains a LinkedList that have a key called nativeHeaders that contains these native headers.
My question: is there a easy way to access this native header? so far, I only get things like

MessageHeaderAccessor accessor = NativeMessageHeaderAccessor.getAccessor(event.getMessage(), SimpMessageHeaderAccessor.class);
accessor.getMessageHeaders();
Object header = accessor.getHeader("simpConnectMessage");
GenericMessage<?> generic = (GenericMessage<?>) accessor.getHeader("simpConnectMessage");
System.out.println(generic.getHeaders().get("nativeHeaders"));

that I really found hard to understand and to use. Actually, the only thing I want is to track the current numbers of users connect to a channel and I think that implement a ApplicationListener is the way. I might be wrong here, so if anyone found something or implemented anything similar, I would like to know

like image 453
Luiz E. Avatar asked Jan 25 '16 23:01

Luiz E.


1 Answers

I did it in such way:

  1. javascript snippet

    function connect() {
        var socket = new SockJS('http://localhost:8081/ws/notification');
        stompClient = Stomp.over(socket);
    
        var header = {
            "user-id" : "bb2c9039-0ed3-4d6f-b078-db965286944d"
        }
    
        stompClient.connect(header, function (frame) {
            console.log('Connected: ' + frame);
            stompClient.subscribe('/user/ws/notification/message', function (greeting) {
                showGreeting(JSON.parse(greeting.body).name);
            });
        });
    }
    
  1. Java snippet

    @EventListener
    public void handleWebSocketConnectListener(SessionConnectedEvent event) {
        StompHeaderAccessor accessor = StompHeaderAccessor.wrap(event.getMessage());
    
        String sessionId = accessor.getMessageHeaders().get(SimpMessageHeaderAccessor.SESSION_ID_HEADER, String.class);
        String userId = getUserId(accessor);
    
        log.info("User( user-id: {} ) established new WebSocket session( session-id: {} ).", userId, sessionId);
        putSessionIdToMap(sessionId, userId);
    }
    
    private String getUserId(StompHeaderAccessor accessor) {
        GenericMessage<?> generic = (GenericMessage<?>) accessor.getHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER);
        if (nonNull(generic)) {
            SimpMessageHeaderAccessor nativeAccessor = SimpMessageHeaderAccessor.wrap(generic);
            List<String> userIdValue = nativeAccessor.getNativeHeader("user-id");
    
            return isNull(userIdValue) ? null : userIdValue.stream().findFirst().orElse(null);
        }
    
        return null;
    }
    
like image 65
Vladimir Avatar answered Sep 20 '22 13:09

Vladimir