【不兼容】WebSocket中onConnected、onClose方法的返回值由void改成CompletableFuture

This commit is contained in:
Redkale
2020-01-08 10:36:54 +08:00
parent d83d7f879c
commit 06bb5180cf
2 changed files with 13 additions and 5 deletions

View File

@@ -740,8 +740,11 @@ public abstract class WebSocket<G extends Serializable, T> {
/**
* WebSokcet连接成功后的回调方法
*
* @return Future 可以为null
*/
public void onConnected() {
public CompletableFuture onConnected() {
return null;
}
/**
@@ -805,8 +808,11 @@ public abstract class WebSocket<G extends Serializable, T> {
*
* @param code 结果码非0表示非正常关闭
* @param reason 关闭原因
*
* @return Future 可以为null
*/
public void onClose(int code, String reason) {
public CompletableFuture onClose(int code, String reason) {
return null;
}
/**

View File

@@ -59,7 +59,8 @@ class WebSocketRunner implements Runnable {
final boolean debug = context.getLogger().isLoggable(Level.FINEST);
final WebSocketRunner self = this;
try {
webSocket.onConnected();
CompletableFuture connectfFuture = webSocket.onConnected();
if (connectfFuture != null) connectfFuture.join();
webSocket._channel.setReadTimeoutSeconds(300); //读取超时5分钟
if (webSocket._channel.isOpen()) {
final int wsmaxbody = webSocket._engine.wsmaxbody;
@@ -307,8 +308,9 @@ class WebSocketRunner implements Runnable {
closed = true;
CompletableFuture<Void> future = engine.removeLocalThenClose(webSocket);
webSocket._channel.dispose();
webSocket.onClose(code, reason);
return future;
CompletableFuture closeFuture = webSocket.onClose(code, reason);
if (closeFuture == null) return future;
return CompletableFuture.allOf(future, closeFuture);
}
}