gpt4 book ai didi

spring - session 关闭时使用 Spring WebSocketConnectionManager 的正确方法是什么

转载 作者:行者123 更新时间:2023-12-02 04:28:51 26 4
gpt4 key购买 nike

我使用 Spring 的 WebSocketConnectionManager使用 WebSocket。有时连接会关闭,我必须重新连接。但我没有找到任何合适的解决方案来做到这一点。我可以使用 WebSocketConnectionManager 恢复(重新连接) session 吗?在 sources I see ,该连接通过以下代码建立:

@Override
protected void openConnection() {
if (logger.isInfoEnabled()) {
logger.info("Connecting to WebSocket at " + getUri());
}

ListenableFuture<WebSocketSession> future =
this.client.doHandshake(this.webSocketHandler, this.headers, getUri());

future.addCallback(new ListenableFutureCallback<WebSocketSession>() {
@Override
public void onSuccess(@Nullable WebSocketSession result) {
webSocketSession = result;
logger.info("Successfully connected");
}
@Override
public void onFailure(Throwable ex) {
logger.error("Failed to connect", ex);
}
});
}

当我的 Spring Boot 应用程序启动时,我正在使用此管理器:

WebSocketConnectionManager connectionManager = new WebSocketConnectionManager(
new StandardWebSocketClient(), webSocketHandler, "wss://localhost:8080/ws/");

connectionManager.start();

但是当连接关闭时我该怎么办?我尝试从 openConnection() 复制一段代码,并使用它直接在 webSocketHandler 中更新 session ,并且它有效。但这看起来像是肮脏的解决方法。

有谁知道正确的做法吗?谢谢。

最佳答案

在我的一个项目中,我遇到了类似的情况。因此,我求助于使用 start() 和 stop() 方法来访问 WebSocketManager 作为连接管理器类中的 bean。我为 websocketserver 创建了一个示例项目和 websocketclient某个时候回来。刚刚添加了重新连接功能。

此外,我并没有在每次连接关闭时重新连接。仅在 close status 1006(异常关闭)、1011(内部错误)、1012(服务重启)。

该项目可以直接下载并运行。

关于spring - session 关闭时使用 Spring WebSocketConnectionManager 的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51873661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com