Websocket chat message does not extend HttpSession

I have a Java Spring application that has a chat endpoint served by a WebSocket. The user is logged in using API calls and the session has a timeout of 10 minutes.

To start Chat, I have to connect to a WebSocket endpoint. It connects, no problem, but the point is: every chat message sent from the client does not extend the login to the HttpSession, so after 10 minutes it disconnects.

How can I make a chat message over a WebSocket connection to extend the HttpSession? Other words say: How can I reset the HttpSession timeout timer to 10 minutes for every message sent over the WebSocket?

Using the reflection method, I get the HttpSession from the WebSocket session and then call the setMaxInactiveInterval () method to reset the session timeout timer, but it doesn't work, the session still disconnects after 10 minutes even though I send a lot of messages in between.

@OnMessage
public void onMessage(Session session, String message) {
    HttpSession httpSession = getHttpSession(session);
    processMessage(message);
    int initialTimeout = httpSession.getMaxInactiveInterval(); // returns 600 (10 mins) 
    httpSession.setMaxInactiveInterval(initialTimeout);
}

      

I need to find a way that Spring expands the session on every API call and probably does it the same way. Does anyone know how Spring does this?

0


source to share


1 answer


Since you are not using HTTP when sending data over a WebSocket connection, the HTTP session will eventually expire and this will also close your WebSocket connections (as described in JSR-356 ).



A simple solution to persist HTTP session when using Spring WebSockets would be using Spring Session .

0


source







All Articles