Asynchronous handshake of a server WebSocket - fixes #2858

This commit is contained in:
Julien Viet
2019-02-28 19:30:22 +01:00
parent d42068a532
commit aa33774041
9 changed files with 211 additions and 40 deletions

View File

@@ -11,7 +11,9 @@
package examples;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
@@ -673,6 +675,27 @@ public class HTTPExamples {
});
}
public void exampleAsynchronousHandshake(HttpServer server) {
server.websocketHandler(websocket -> {
Future<Integer> fut = Future.future();
websocket.setHandshake(fut);
authenticate(websocket, ar -> {
if (ar.succeeded()) {
// Terminate the handshake with the status code 101 (Switching Protocol)
// Reject the handshake with 401 (Unauthorized)
fut.complete(ar.succeeded() ? 101 : 401);
} else {
// Will send a 500 error
fut.fail(ar.cause());
}
});
});
}
private static void authenticate(ServerWebSocket ws, Handler<AsyncResult<Boolean>> handler) {
}
public void example53(HttpServer server) {
server.requestHandler(request -> {