M
M
Mark Ivanych2020-03-03 17:42:58
Java
Mark Ivanych, 2020-03-03 17:42:58

How to return connection session ID in Spring + STOMP?

Hello, I need to get the SessionId when a socket connection occurs.

What SessionId are we talking about, what identifies the socket connection.
An example in which you can get this very SessionId

@MessageMapping("/find")
public void find(SimpMessageHeaderAccessor sha, User user) {
    String sessionId = sha.getSessionId();
    LOG.debug("New session id - {}, {}", sessionId, user);
    // New session id - rs5ahsly, {user: Name, ...}
    Repository.add(sessionId, user);
}

Knowing the SessionId, you can send a message to a specific subscriber.

The question is, I need to return the SessionId when a connection to the server is established via the STOMP protocol. This is the response I want to receive from the server:
CONNECTED↵version:1.1↵heart-beat:0,0↵sessionId:rs5ahsly↵↵


For example, I can generate a unique user ID key with the code below, but this will return randomUUID
public class CustomHandshakeHandler extends DefaultHandshakeHandler {    
    @Override
    protected Principal determineUser(ServerHttpRequest request,
                                      WebSocketHandler wsHandler,
                                      Map<String, Object> attributes) {
        return new StompPrincipal(UUID.randomUUID().toString());
    }
}

How can I do something like this but return the socket's SessionId?
In my opinion, there is no access to SessionId from DefaultHandshakeHandler

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question