mirror of
https://github.com/jlengrand/vert.x.git
synced 2026-04-11 00:41:22 +00:00
Http2 server doc and examples
This commit is contained in:
201
src/main/java/examples/HTTP2Examples.java
Normal file
201
src/main/java/examples/HTTP2Examples.java
Normal file
@@ -0,0 +1,201 @@
|
||||
/*
|
||||
* Copyright (c) 2011-2013 The original author or authors
|
||||
* ------------------------------------------------------
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Apache License v2.0 which accompanies this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* The Apache License v2.0 is available at
|
||||
* http://www.opensource.org/licenses/apache2.0.php
|
||||
*
|
||||
* You may elect to redistribute this code under either of these licenses.
|
||||
*/
|
||||
|
||||
package examples;
|
||||
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
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.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.unknownFrameHandler(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.writeFrame(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()) {
|
||||
|
||||
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>");
|
||||
}
|
||||
|
||||
public void example7(Vertx vertx) {
|
||||
|
||||
HttpClientOptions options = new HttpClientOptions().
|
||||
setProtocolVersion(HttpVersion.HTTP_2).
|
||||
setSsl(true).
|
||||
setUseAlpn(true).
|
||||
setTrustAll(true);
|
||||
|
||||
HttpClient client = vertx.createHttpClient(options);
|
||||
}
|
||||
|
||||
public void example8(Vertx vertx) {
|
||||
|
||||
HttpClientOptions options = new HttpClientOptions().setProtocolVersion(HttpVersion.HTTP_2);
|
||||
|
||||
HttpClient client = vertx.createHttpClient(options);
|
||||
}
|
||||
|
||||
public void example9(HttpClientRequest request) {
|
||||
|
||||
int frameType = 40;
|
||||
int frameStatus = 10;
|
||||
Buffer payload = Buffer.buffer("some data");
|
||||
|
||||
// Sending a frame to the server
|
||||
request.writeFrame(frameType, frameStatus, payload);
|
||||
}
|
||||
|
||||
public void example10(HttpClientRequest request) {
|
||||
|
||||
request.reset();
|
||||
|
||||
}
|
||||
|
||||
public void example11(HttpClientRequest request) {
|
||||
|
||||
request.reset(8);
|
||||
|
||||
}
|
||||
|
||||
public void example12(HttpClientRequest request) {
|
||||
|
||||
request.exceptionHandler(err -> {
|
||||
if (err instanceof StreamResetException) {
|
||||
StreamResetException reset = (StreamResetException) err;
|
||||
System.out.println("Stream reset " + reset.getCode());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void example13(HttpClient client) {
|
||||
|
||||
HttpClientRequest request = client.get("/index.html", response -> {
|
||||
// Process index.html response
|
||||
});
|
||||
|
||||
// Set a push handler to be aware of any resource pushed by the server
|
||||
request.pushHandler(pushedRequest -> {
|
||||
|
||||
// A resource is pushed for this request
|
||||
System.out.println("Server pushed " + pushedRequest.path());
|
||||
|
||||
// Set an handler for the response
|
||||
pushedRequest.handler(pushedResponse -> {
|
||||
System.out.println("The response for the pushed request");
|
||||
});
|
||||
});
|
||||
|
||||
// End the request
|
||||
request.end();
|
||||
}
|
||||
|
||||
public void example13(HttpClientRequest request) {
|
||||
request.pushHandler(pushedRequest -> {
|
||||
if (pushedRequest.path().equals("/main.js")) {
|
||||
pushedRequest.reset();
|
||||
} else {
|
||||
// Handle it
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void example15(HttpClientResponse response) {
|
||||
response.unknownFrameHandler(frame -> {
|
||||
|
||||
System.out.println("Received a frame type=" + frame.type() +
|
||||
" payload" + frame.payload().toString());
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user