Websocket 聊天消息不会延长 HttpSession [英] Websocket chat message does not prolong HttpSession

查看:57
本文介绍了Websocket 聊天消息不会延长 HttpSession的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Java Spring 应用程序,它通过 WebSocket 提供聊天端点.用户使用 API 调用登录,会话超时 10 分钟.

I have Java Spring Application, that has Chat endpoint served though WebSocket. User logs in using API calls and session has timeout 10 mins.

为了开始聊天,我必须连接到 WebSocket 端点.它确实连接了,没问题,但问题是:从客户端发送的每个聊天消息都不会延长登录 HttpSession 的时间,因此在 10 分钟后超时.

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.

如何通过 WebSocket 连接发送聊天消息以延长 HttpSession?换句话说:如何在通过 WebSocket 发送的每条消息上将 HttpSession 超时计时器重置为 10 分钟?

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?

使用反射方法从 WebSocket 会话中获取 HttpSession,然后调用 setMaxInactiveInterval() 方法来重置会话超时计时器,但它不起作用,会话在 10 分钟后仍然超时,即使我在两者之间发送了许多消息.

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);
}

我需要找到 Spring 在每个 API 调用上扩展会话的方式,并且可能以相同的方式进行.有谁知道Spring是怎么做的?

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?

推荐答案

由于您在通过 WebSocket 连接发送数据时没有使用 HTTP,因此 HTTP 会话最终会超时,这也会使您的 WebSocket 连接关闭(如JSR-356).

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).

在使用 Spring WebSockets 时保持 HTTP 会话处于活动状态的一个简单解决方案是使用 Spring Session 沿.

An easy solution to keep the HTTP session alive when using Spring WebSockets would be using Spring Session along.

这篇关于Websocket 聊天消息不会延长 HttpSession的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆