【问题标题】:Spring websocket timeout settingsSpring websocket超时设置
【发布时间】:2014-11-13 20:01:01
【问题描述】:

我正在使用 Spring websocket 支持。我的问题是如何设置 websocket 连接超时。现在连接会在几分钟后自动关闭。我希望连接永远不会关闭。

这是我的 websocket 处理程序:

public class MyHandler implements WebSocketHandler {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    class MyTimerTask extends TimerTask {
        private WebSocketSession session;
        public MyTimerTask(WebSocketSession session) {
            this.session = session;
        }

        @Override
        public void run() {
            try {
                String msg = ((int)(Math.random()*50)) + "";
                this.session.sendMessage(new TextMessage(msg.toString()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Autowired
    private UserDao userDao;

    @Autowired
    private JdbcDaoImpl jdbcDaoImpl;
    private Timer timer;

    @Override
    public void afterConnectionEstablished(WebSocketSession session)
            throws Exception {
        System.out.println("websocket????");
        timer = new Timer();
        timer.schedule(new MyTimerTask(session), 0, 1000);
        logger.info("logger connection");
    }

    @Override
    public void handleMessage(WebSocketSession session,
            WebSocketMessage<?> message) throws Exception { }

    @Override
    public void handleTransportError(WebSocketSession session,
            Throwable exception) throws Exception { }

    @Override
    public void afterConnectionClosed(WebSocketSession session,
            CloseStatus closeStatus) throws Exception {
        System.out.println("websocket????");
        timer.cancel();
    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }   
}

我的 websocket 配置:

<websocket:handlers>
    <websocket:mapping path="/myHandler" handler="myHandler"/>
</websocket:handlers>

<bean id="myHandler" class="com.sdp.websocket.MyHandler"/>

和javascript客户端:

var webserver = 'ws://localhost:8080/authtest/myHandler';
var websocket = new WebSocket(webserver);
websocket.onopen = function (evt) { onOpen(evt) }; 
websocket.onclose = function (evt) { onClose(evt) }; 
websocket.onmessage = function (evt) { onMessage(evt) }; 
websocket.onerror = function (evt) { onError(evt) }; 

function onOpen(evt) { 
    console.log("Connected to WebSocket server."); 
} 

function onClose(evt) { 
    console.log("Disconnected"); 
} 

function onMessage(evt) { 
    console.log('Retrieved data from server: ' + evt.data); 
} 

function onError(evt) { 
    console.log('Error occured: ' + evt.data); 
}
debugger;
function sendMsg(){
    websocket.send("{msg:'hello'}");
}

【问题讨论】:

    标签: spring websocket timeout


    【解决方案1】:

    websocket 保持打开状态,直到服务器或客户端决定关闭它。然而,websockets 会受到两个超时的影响:

    • HTTP 会话超时;
    • 代理连接超时;

    如果您的客户端和服务器之间只有一个 websocket 连接,并且您没有通过 HTTP 与 AJAX 交互或请求其他页面,那么 HTTP 会话将过期并且一些服务器决定将其与 websocket 一起失效(Tomcat7有一个错误就是这样做的)。其他一些服务器不这样做,因为他们看到 websocket 上有活动。请参阅此处进行详细讨论:Need some resolution or clarification for how and when HttpSession last access time gets updated

    另一个超时是代理。他们看到了连接,如果在很长一段时间内电线上没有任何活动,他们就会切断它,因为他们认为它挂了。为了解决这个问题,虽然您不发送实际的应用程序数据,但您需要不时发送心跳或 ping/pong 消息,让代理知道连接仍然正常。

    其他问题也可能会介入,例如浏览器对 websocket 的支持有问题、您的网络是如何配置的、防火墙规则等。

    有关 Spring 中可用的超时选项,请参阅 websocket 文档:Configuring the WebSocket Engine

    【讨论】:

    • 是的,你是对的。由 Tomcat 的 session 和 cookie 超时决定。我在 web.xml 中设置了超时
    • 查看 Spring Session (github.com/spring-projects/spring-session),它在 WebSocket 处于活动状态时保持 HttpSession 处于活动状态
    • 您是否更改了tomcat的web.xml文件中的'session-timeout'来解决这个问题?
    • 这个答案对我帮助很大,我实现了一个乒乓机制,现在我的应用程序运行良好,所以谢谢你:) :) :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 2018-08-06
    • 2017-02-15
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    相关资源
    最近更新 更多