Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: complete client websocket future on open (#20587) (CP: 24.6) #20662

Merged
merged 1 commit into from
Dec 10, 2024
Merged
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 @@ -42,7 +42,7 @@ public class ViteWebsocketConnection implements Listener {

private final Consumer<String> onMessage;
private final Runnable onClose;
private final CompletableFuture<WebSocket> clientWebsocket;
private final CompletableFuture<WebSocket> clientWebsocket = new CompletableFuture<>();
private final List<CharSequence> parts = new ArrayList<>();

private static Logger getLogger() {
Expand Down Expand Up @@ -72,15 +72,20 @@ public ViteWebsocketConnection(int port, String path, String subProtocol,
this.onClose = onClose;
String wsHost = ViteHandler.DEV_SERVER_HOST.replace("http://", "ws://");
URI uri = URI.create(wsHost + ":" + port + path);
clientWebsocket = HttpClient.newHttpClient().newWebSocketBuilder()
HttpClient.newHttpClient().newWebSocketBuilder()
.subprotocols(subProtocol).buildAsync(uri, this)
.whenComplete(((webSocket, failure) -> {
if (failure == null) {
getLogger().debug(
"Connection to {} using the {} protocol established",
uri, webSocket.getSubprotocol());
if (clientWebsocket.complete(webSocket)) {
getLogger().trace(
"Websocket future completed in client build completion");
}
} else {
getLogger().debug("Failed to connect to {}", uri);
clientWebsocket.completeExceptionally(failure);
onConnectionFailure.accept(failure);
}
}));
Expand All @@ -90,6 +95,9 @@ public ViteWebsocketConnection(int port, String path, String subProtocol,
public void onOpen(WebSocket webSocket) {
getLogger().debug("Connected using the {} protocol",
webSocket.getSubprotocol());
if (clientWebsocket.complete(webSocket)) {
getLogger().trace("Websocket future completed in onOpen");
}
Listener.super.onOpen(webSocket);
}

Expand Down