SHARE
TWEET

Untitled

a guest May 21st, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package examples;
  2.  
  3. import io.vertx.core.Vertx;
  4. import io.vertx.core.buffer.Buffer;
  5. import io.vertx.core.http.Http2Settings;
  6. import io.vertx.core.http.HttpClient;
  7. import io.vertx.core.http.HttpClientOptions;
  8. import io.vertx.core.http.HttpClientRequest;
  9. import io.vertx.core.http.HttpClientResponse;
  10. import io.vertx.core.http.HttpConnection;
  11. import io.vertx.core.http.HttpMethod;
  12. import io.vertx.core.http.HttpServer;
  13. import io.vertx.core.http.HttpServerOptions;
  14. import io.vertx.core.http.HttpServerRequest;
  15. import io.vertx.core.http.HttpServerResponse;
  16. import io.vertx.core.http.HttpVersion;
  17. import io.vertx.core.http.StreamResetException;
  18. import io.vertx.core.net.JksOptions;
  19.  
  20. /**
  21.  * @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
  22.  */
  23. public class HTTP2Examples {
  24.  
  25.     public void example0(Vertx vertx) {
  26.         HttpServerOptions options = new HttpServerOptions()
  27.                 .setUseAlpn(true)
  28.                 .setSsl(true)
  29.                 .setKeyStoreOptions(new JksOptions().setPath("/path/to/my/keystore"));
  30.  
  31.         HttpServer server = vertx.createHttpServer(options);
  32.     }
  33.  
  34.     public void example1(HttpServerRequest request) {
  35.  
  36.         request.customFrameHandler(frame -> {
  37.  
  38.             System.out.println("Received a frame type=" + frame.type() +
  39.                     " payload" + frame.payload().toString());
  40.         });
  41.     }
  42.  
  43.     public void example2(HttpServerResponse response) {
  44.  
  45.         int frameType = 40;
  46.         int frameStatus = 10;
  47.         Buffer payload = Buffer.buffer("some data");
  48.  
  49.         // Sending a frame to the client
  50.         response.writeCustomFrame(frameType, frameStatus, payload);
  51.     }
  52.  
  53.     public void example3(HttpServerRequest request) {
  54.  
  55.         // Reset the stream
  56.         request.response().reset();
  57.     }
  58.  
  59.     public void example4(HttpServerRequest request) {
  60.  
  61.         // Cancel the stream
  62.         request.response().reset(8);
  63.     }
  64.  
  65.     public void example5(HttpServerRequest request) {
  66.  
  67.         request.response().exceptionHandler(err -> {
  68.             if (err instanceof StreamResetException) {
  69.                 StreamResetException reset = (StreamResetException) err;
  70.                 System.out.println("Stream reset " + reset.getCode());
  71.             }
  72.         });
  73.     }
  74.  
  75.     public void example6(HttpServerRequest request) {
  76.  
  77.         HttpServerResponse response = request.response();
  78.  
  79.         // Push main.js to the client
  80.         response.push(HttpMethod.GET, "/main.js", ar -> {
  81.  
  82.             if (ar.succeeded()) {
  83.  
  84.                 // The server is ready to push the response
  85.                 HttpServerResponse pushedResponse = ar.result();
  86.  
  87.                 // Send main.js response
  88.                 pushedResponse.
  89.                         putHeader("content-type", "application/json").
  90.                         end("alert(\"Push response hello\")");
  91.             } else {
  92.                 System.out.println("Could not push client resource " + ar.cause());
  93.             }
  94.         });
  95.  
  96.         // Send the requested resource
  97.         response.sendFile("<html><head><script src=\"/main.js\"></script></head><body></body></html>");
  98.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top