Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Session Id in Spring WebSocketStompClient?

How to get session id in Java Spring WebSocketStompClient?

I have WebSocketStompClient and StompSessionHandlerAdapter, which instances connect fine to websocket on my server. WebSocketStompClient use SockJsClient.
But I don't know how get session id of websocket connection. In the code with stomp session handler on client side

   private class ProducerStompSessionHandler extends StompSessionHandlerAdapter {
            ...
            @Override
            public void afterConnected(StompSession session, StompHeaders  connectedHeaders) {
            ...
            }

stomp session contains session id, which is different from session id on the server. So from this ids:

DEBUG ... Processing SockJS open frame in WebSocketClientSockJsSession[id='d6aaeacf90b84278b358528e7d96454a...

DEBUG ... DefaultStompSession - Connection established in session id=42e95c88-cbc9-642d-2ff9-e5c98fb85754

I need first session id, from WebSocketClientSockJsSession. But I didn't found in WebSocketStompClient or SockJsClient any method to retrieve something like session id...

like image 379
Irina Avatar asked Feb 15 '17 07:02

Irina


People also ask

What is @EnableWebSocketMessageBroker?

Annotation Type EnableWebSocketMessageBrokerAdd this annotation to an @Configuration class to enable broker-backed messaging over WebSocket using a higher-level messaging sub-protocol.

How do I find my Spring boot session ID?

getSessionId(); This relies on Spring's RequestContextHolder , so it should be used with Spring MVC's DispatcherServlet or you should have a RequestContextListener declared. Also session will be created if not exists. @axtavt I have angular appli running in diffrnt port and server code is running in diffrnt port .

How do I find my WebSocket session ID?

To get session id we need to make some changes into SockJS library. And if we need to know session id before calling connect method we can modify SockJS' constructor and connect method to use client-passed value.

What is registerStompEndpoints?

The registerStompEndpoints() method registers the /gs-guide-websocket endpoint, enabling SockJS fallback options so that alternate transports can be used if WebSocket is not available.


2 Answers

You can use @Header annotation to access sessionId:

@MessageMapping("/login")
public void login(@Header("simpSessionId") String sessionId) {
    System.out.println(sessionId);
}

And it works fine for me without any custom interceptors

like image 197
Arseniy Avatar answered Oct 07 '22 21:10

Arseniy


To get session id you need to define your own interceptor as below and set the session id as a custom attribute.

public class HttpHandshakeInterceptor implements HandshakeInterceptor {

    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
            Map attributes) throws Exception {
        if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
            HttpSession session = servletRequest.getServletRequest().getSession();
            attributes.put("sessionId", session.getId());
        }
        return true;
    }

Now you can get the same session id in the controller class.

@MessageMapping("/message")
public void processMessageFromClient(@Payload String message, SimpMessageHeaderAccessor  headerAccessor) throws Exception {
        String sessionId = headerAccessor.getSessionAttributes().get("sessionId").toString();

    }
like image 20
Dhiraj Ray Avatar answered Oct 07 '22 22:10

Dhiraj Ray