I have Java Spring Application, that has Chat endpoint served though WebSocket. User logs in using API calls and session has timeout 10 mins.
In order to start Chat I have to connect to WebSocket endpoint. It does connect, no problem, but the thing is that: each chat message sent from client does not prolong logged in HttpSession, so after 10 mins it times out.
How can I make chat message via WebSocket connecting to prolong HttpSession? Other words saying: how can I reset HttpSession timeout timer to 10 mins on each message sent via WebSocket?
Using reflection method I get HttpSession from WebSocket session and then I call setMaxInactiveInterval() method to reset session timeout timer, but it does not work, session still times out after 10 mins, even if I send many 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 the way Spring extends the session on each API call and probably do it same way. Does anyone knows how Spring does it?
As you are not using HTTP when sending data through a WebSocket connection, the HTTP session will eventually timeout and this will also make your WebSocket connections close (as described in the JSR-356).
An easy solution to keep the HTTP session alive when using Spring WebSockets would be using Spring Session along.