Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package examples;
- import io.vertx.core.Vertx;
- import io.vertx.core.buffer.Buffer;
- import io.vertx.core.http.Http2Settings;
- import io.vertx.core.http.HttpClient;
- import io.vertx.core.http.HttpClientOptions;
- import io.vertx.core.http.HttpClientRequest;
- import io.vertx.core.http.HttpClientResponse;
- import io.vertx.core.http.HttpConnection;
- import io.vertx.core.http.HttpMethod;
- import io.vertx.core.http.HttpServer;
- import io.vertx.core.http.HttpServerOptions;
- import io.vertx.core.http.HttpServerRequest;
- import io.vertx.core.http.HttpServerResponse;
- import io.vertx.core.http.HttpVersion;
- import io.vertx.core.http.StreamResetException;
- import io.vertx.core.net.JksOptions;
- /**
- * @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
- */
- public class HTTP2Examples {
- public void example0(Vertx vertx) {
- HttpServerOptions options = new HttpServerOptions()
- .setUseAlpn(true)
- .setSsl(true)
- .setKeyStoreOptions(new JksOptions().setPath("/path/to/my/keystore"));
- HttpServer server = vertx.createHttpServer(options);
- }
- public void example1(HttpServerRequest request) {
- request.customFrameHandler(frame -> {
- System.out.println("Received a frame type=" + frame.type() +
- " payload" + frame.payload().toString());
- });
- }
- public void example2(HttpServerResponse response) {
- int frameType = 40;
- int frameStatus = 10;
- Buffer payload = Buffer.buffer("some data");
- // Sending a frame to the client
- response.writeCustomFrame(frameType, frameStatus, payload);
- }
- public void example3(HttpServerRequest request) {
- // Reset the stream
- request.response().reset();
- }
- public void example4(HttpServerRequest request) {
- // Cancel the stream
- request.response().reset(8);
- }
- public void example5(HttpServerRequest request) {
- request.response().exceptionHandler(err -> {
- if (err instanceof StreamResetException) {
- StreamResetException reset = (StreamResetException) err;
- System.out.println("Stream reset " + reset.getCode());
- }
- });
- }
- public void example6(HttpServerRequest request) {
- HttpServerResponse response = request.response();
- // Push main.js to the client
- response.push(HttpMethod.GET, "/main.js", ar -> {
- if (ar.succeeded()) {
- // The server is ready to push the response
- HttpServerResponse pushedResponse = ar.result();
- // Send main.js response
- pushedResponse.
- putHeader("content-type", "application/json").
- end("alert(\"Push response hello\")");
- } else {
- System.out.println("Could not push client resource " + ar.cause());
- }
- });
- // Send the requested resource
- response.sendFile("<html><head><script src=\"/main.js\"></script></head><body></body></html>");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement