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
8 changes: 5 additions & 3 deletions src/main/java/io/vertx/core/http/WebSocketClientOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,14 @@ public int getClosingTimeout() {
}

/**
* Set the amount of time a client WebSocket will wait until it closes the TCP connection after receiving a close frame.
* Set the amount of time a client WebSocket will wait until it closes the TCP connection after sending a close frame.
*
* <p> When a WebSocket is closed, the server should close the TCP connection. This timeout will close
* the TCP connection on the client when it expires.
* the TCP connection on the client when the server has not responded with a close frame and closed the connection
* in a timely manner.
*
* <p> Set to {@code 0L} closes the TCP connection immediately after receiving the close frame.
* <p> Setting to {@code 0L} closes the TCP connection after receiving the close frame, note this should
* not be avoided when interacting with misbehaving server that do not respond with close frames.
*
* <p> Set to a negative value to disable it.
*
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/io/vertx/core/http/impl/WebSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

package io.vertx.core.http.impl;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelPromise;
import io.vertx.core.Future;
import io.vertx.core.http.WebSocket;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.spi.metrics.HttpClientMetrics;
Expand Down Expand Up @@ -47,11 +51,22 @@ public WebSocketImpl(ContextInternal context,
protected void handleCloseConnection() {
if (closingTimeoutMS == 0L) {
closeConnection();
} else if (closingTimeoutMS > 0L) {
initiateConnectionCloseTimeout(closingTimeoutMS);
}
}

@Override
protected ChannelPromise sendCloseFrame(short statusCode, String reason) {
ChannelPromise promise = super.sendCloseFrame(statusCode, reason);
if (closingTimeoutMS > 0L) {
promise.addListener((ChannelFutureListener) channelFuture -> {
if (channelFuture.isSuccess()) {
initiateConnectionCloseTimeout(closingTimeoutMS);
}
});
}
return promise;
}

@Override
protected void handleClose(boolean graceful) {
HttpClientMetrics metrics = conn.metrics();
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/io/vertx/core/http/impl/WebSocketImplBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,23 +182,31 @@ public void close(short statusCode, @Nullable String reason, Handler<AsyncResult
public Future<Void> close(short statusCode, String reason) {
boolean sendCloseFrame;
synchronized (conn) {
if (sendCloseFrame = closeStatusCode == null) {
closeStatusCode = statusCode;
closeReason = reason;
sendCloseFrame = closeStatusCode == null;
if (sendCloseFrame) {
closeStatusCode = 1006;
closeReason = null;
}
}
if (sendCloseFrame) {
// Close the WebSocket by sending a close frame with specified payload
ByteBuf byteBuf = HttpUtils.generateWSCloseFrameByteBuf(statusCode, reason);
CloseWebSocketFrame frame = new CloseWebSocketFrame(true, 0, byteBuf);
ChannelPromise channelPromise = sendCloseFrame(statusCode, reason);
PromiseInternal<Void> promise = context.promise();
conn.writeToChannel(frame, promise);
return promise;
channelPromise.addListener(promise);
return promise.future();
} else {
return context.succeededFuture();
}
}

protected ChannelPromise sendCloseFrame(short statusCode, String reason) {
// Close the WebSocket by sending a close frame with specified payload
ByteBuf byteBuf = HttpUtils.generateWSCloseFrameByteBuf(statusCode, reason);
CloseWebSocketFrame frame = new CloseWebSocketFrame(true, 0, byteBuf);
ChannelPromise promise = conn.channelFuture();
conn.writeToChannel(frame, promise);
return promise;
}

@Override
public boolean isSsl() {
return conn.isSsl();
Expand Down
21 changes: 16 additions & 5 deletions src/test/java/io/vertx/core/http/WebSocketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3157,15 +3157,20 @@ public void testClientCloseHandshake() {

@Test
public void testClientConnectionCloseTimeout() {
testClientConnectionCloseTimeout(1);
testClientConnectionCloseTimeout(1, true, 1000);
}

@Test
public void testClientConnectionCloseImmediately() {
testClientConnectionCloseTimeout(0);
testClientConnectionCloseTimeout(0, true, 1000);
}

public void testClientConnectionCloseTimeout(int timeout) {
@Test
public void testClientConnectionCloseTimeoutWithoutCloseFrame() {
testClientConnectionCloseTimeout(1, false, 1006);
}

public void testClientConnectionCloseTimeout(int timeout, boolean respondWithCloseFrame, int expectedStatusCode) {
waitFor(3);
List<Object> received = Collections.synchronizedList(new ArrayList<>());
server = vertx.createHttpServer();
Expand All @@ -3176,7 +3181,7 @@ public void testClientConnectionCloseTimeout(int timeout) {
soi.channelHandlerContext().pipeline().addBefore("handler", "decoder", new WebSocket13FrameDecoder(true, false, 1000));
soi.messageHandler(msg -> {
received.add(msg);
if (msg instanceof CloseWebSocketFrame) {
if (msg instanceof CloseWebSocketFrame && respondWithCloseFrame) {
CloseWebSocketFrame frame = (CloseWebSocketFrame) msg;
soi.writeMessage(new CloseWebSocketFrame(frame.statusCode(), frame.reasonText()));
}
Expand All @@ -3190,13 +3195,19 @@ public void testClientConnectionCloseTimeout(int timeout) {
client = vertx.createWebSocketClient(new WebSocketClientOptions().setClosingTimeout(timeout));
client.connect(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/chat", onSuccess(ws -> {
ws.endHandler(v -> {
assertTrue(respondWithCloseFrame);
complete();
});
ws.exceptionHandler(err -> {
assertFalse(respondWithCloseFrame);
complete();
});
ws.exceptionHandler(err -> fail());
ws.closeHandler(v -> {
assertEquals(1, received.size());
Object msg = received.get(0);
try {
assertNotNull(ws.closeStatusCode());
assertEquals(expectedStatusCode, (short)ws.closeStatusCode());
assertEquals(msg.getClass(), CloseWebSocketFrame.class);
} finally {
ReferenceCountUtil.release(msg);
Expand Down
Loading