你怎么设置org.apache.tomcat.websocket.BLOCKING_SEND_TIMEOUT?

How can you set org.apache.tomcat.websocket.BLOCKING_SEND_TIMEOUT?

设置 org.apache.tomcat.websocket.BLOCKING_SEND_TIMEOUT 以增加 tomcat websocket 超时的预期方法是什么? Tomcat 文档说明如下:

This may be changed by setting the property org.apache.tomcat.websocket.BLOCKING_SEND_TIMEOUT in the user properties collection attached to the WebSocket session.

我在 TextWebSocketHandler 的 afterConnectionEstablished 方法中看到的 WebSocketSession 似乎没有用户属性。所以,我认为这不是文档的意思。在查看 TomcatRequestUpgradeStrategy 时,在我看来它从不查看端点用户属性。在我看来,您也无法覆盖 TomcatRequestUpgradeStrategy,因为 AbstractHandshakeHandler 具有 TomcatRequestUpgradeStrategy 的硬编码 class 名称。

请帮忙。

org.apache.tomcat.websocket.BLOCKING_SEND_TIMEOUT 是一个用户 属性 你需要在 WebSocket API 的 Session 上设置,而不是这个接口的 Spring 抽象。

您可以在 afterConnectionEstablished 方法中配置它,方法是将 Spring WebSocketSession 转换为 NativeWebSocketSession 并检索基础 WebSocket API 会话:

public void afterConnectionEstablished(WebSocketSession session) throws Exception {
    if (session instanceof NativeWebSocketSession) {
        final Session nativeSession = ((NativeWebSocketSession) session).getNativeSession(Session.class);
        if (nativeSession != null) {
            nativeSession.getUserProperties()
                         .put("org.apache.tomcat.websocket.BLOCKING_SEND_TIMEOUT", 60_000L);
        }
    }
}