【发布时间】:2019-01-23 05:07:59
【问题描述】:
我使用 Spring 的 WebSocketConnectionManager 来处理 WebSockets。有时连接关闭,我必须重新连接。但我没有找到任何合适的解决方案来做到这一点。我可以使用 WebSocketConnectionManager 来恢复(重新连接)会话吗?在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 中更新会话,并且它可以工作。但它看起来像是肮脏的解决方法。
有人知道如何正确操作吗?谢谢。
【问题讨论】:
标签: spring spring-boot websocket spring-websocket