Skip to content

Commit

Permalink
Provide opportunity to create a client WebSocket with a given vertx c…
Browse files Browse the repository at this point in the history
…ontext to use.
  • Loading branch information
vietj committed Oct 24, 2024
1 parent 032de2b commit a7c2ba5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
12 changes: 10 additions & 2 deletions src/main/java/io/vertx/core/http/impl/ClientWebSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* Client WebSocket implementation
*/
public class ClientWebSocketImpl implements ClientWebSocket {
public class ClientWebSocketImpl implements ClientWebSocketInternal {

private HttpClientBase client;
private final AtomicReference<Promise<WebSocket>> connect = new AtomicReference<>();
Expand All @@ -48,7 +48,15 @@ public class ClientWebSocketImpl implements ClientWebSocket {

@Override
public Future<WebSocket> connect(WebSocketConnectOptions options) {
ContextInternal ctx = client.vertx().getOrCreateContext();
return connect(client.vertx().getOrCreateContext(), options);
}

@Override
public Future<WebSocket> connect(Context context, WebSocketConnectOptions options) {
return connect((ContextInternal) context, options);
}

private Future<WebSocket> connect(ContextInternal ctx, WebSocketConnectOptions options) {
Promise<WebSocket> promise = ctx.promise();
if (!connect.compareAndSet(null, promise)) {
return ctx.failedFuture("Already connecting");
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/io/vertx/core/http/impl/ClientWebSocketInternal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2011-2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.core.http.impl;

import io.vertx.core.Context;
import io.vertx.core.Future;
import io.vertx.core.http.ClientWebSocket;
import io.vertx.core.http.WebSocket;
import io.vertx.core.http.WebSocketConnectOptions;

/**
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
public interface ClientWebSocketInternal extends ClientWebSocket {

Future<WebSocket> connect(Context context, WebSocketConnectOptions options);

}
10 changes: 0 additions & 10 deletions src/main/java/io/vertx/core/http/impl/HttpClientBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,6 @@ public void webSocket(WebSocketConnectOptions connectOptions, Handler<AsyncResul
webSocket(connectOptions, promise);
}

Future<WebSocket> webSocket(ContextInternal ctx, WebSocketConnectOptions connectOptions) {
PromiseInternal<WebSocket> promise = ctx.promise();
webSocket(connectOptions, promise);
return promise.andThen(ar -> {
if (ar.succeeded()) {
ar.result().resume();
}
});
}

private void webSocket(WebSocketConnectOptions connectOptions, PromiseInternal<WebSocket> promise) {
ContextInternal ctx = promise.context();
int port = getPort(connectOptions);
Expand Down

0 comments on commit a7c2ba5

Please sign in to comment.