Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public VertxWebsocketEndpoint getEndpoint() {

@Override
protected void doStart() throws Exception {
configureWebSocketHandlers(getEndpoint().getWebSocket());
getEndpoint().getWebSocket(this::configureWebSocketHandlers);
}

protected void configureWebSocketHandlers(WebSocket webSocket) {
Expand All @@ -58,7 +58,7 @@ protected void configureWebSocketHandlers(WebSocket webSocket) {
Vertx vertx = getEndpoint().getVertx();
vertx.setPeriodic(configuration.getReconnectInitialDelay(), configuration.getReconnectInterval(), timerId -> {
vertx.executeBlocking(() -> {
configureWebSocketHandlers(getEndpoint().getWebSocket());
getEndpoint().getWebSocket(this::configureWebSocketHandlers);
vertx.cancelTimer(timerId);
return null;
}, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ protected Vertx getVertx() {
}

protected WebSocket getWebSocket() throws Exception {
return getWebSocket((java.util.function.Consumer<WebSocket>) null);
}

/**
* Connects to the WebSocket server, optionally invoking a setup callback on the socket <em>before</em> the blocking
* {@code future.get()} returns. This guarantees that any message/close/exception handlers supplied by the callback
* are registered atomically with the connection, so no messages sent by the server immediately after the handshake
* can be missed.
*/
protected WebSocket getWebSocket(java.util.function.Consumer<WebSocket> setupCallback) throws Exception {
if (client == null) {
resolvedClientOptions = configuration.getClientOptions();
if (resolvedClientOptions == null) {
Expand All @@ -171,8 +181,17 @@ protected WebSocket getWebSocket() throws Exception {
CompletableFuture<WebSocket> future = new CompletableFuture<>();
client.connect(connectOptions).onComplete(result -> {
if (result.succeeded()) {
LOG.info("Connected to WebSocket on {}", result.result().remoteAddress());
future.complete(result.result());
WebSocket ws = result.result();
LOG.info("Connected to WebSocket on {}", ws.remoteAddress());
if (setupCallback != null) {
try {
setupCallback.accept(ws);
} catch (Exception e) {
future.completeExceptionally(e);
return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor formatting nit — missing space before the parenthesis:

Suggested change
return;
} catch (Exception e) {

}
}
future.complete(ws);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor robustness suggestion: if setupCallback.accept(ws) throws, future.complete(ws) is never reached and the caller gets a TimeoutException rather than the real cause. Consider:

Suggested change
future.complete(ws);
if (setupCallback != null) {
try {
setupCallback.accept(ws);
} catch (Exception e) {
future.completeExceptionally(e);
return;
}
}

Low-risk in practice since configureWebSocketHandlers only calls handler setters, but would help debugging if this pattern is reused elsewhere.

} else {
webSocket = null;
future.completeExceptionally(result.cause());
Expand Down