@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end(new JsonObject().put("time", System.currentTimeMillis()).toString()); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end(new JsonObject().put("time", System.currentTimeMillis()).toString()); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "text/html").end("<html><body><h1>Hello from vert.x!</h1></body></html>"); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end("{\"message\":\"Hello World\"}"); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end("{\"message\":\"Hello World\"}"); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "text/html").end("<html><body><h1>Hello from vert.x!</h1></body></html>"); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "text/html").end("<html><body><h1>Hello from vert.x!</h1></body></html>"); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { HttpServerResponse resp = req.response(); String contentType = req.getHeader("Content-Type"); if (contentType != null) { resp.putHeader("Content-Type", contentType); } resp.setChunked(true); req.toObservable().subscribe( resp::write, err -> {}, resp::end ); }); server.listen(8080); } }
@Override public void start() throws Exception { Router router = Router.router(vertx); router.route().handler(routingContext -> { routingContext.response().putHeader("content-type", "text/html").end("Hello World!"); }); vertx.createHttpServer().requestHandler(router).listen(8080); } }
@Override public void start() throws Exception { Router router = Router.router(vertx); router.route("/news-feed/*").handler(SockJSHandler.create(vertx).socketHandler(sockJSSocket -> { // Consumer the event bus address as an Observable Observable<String> msg = vertx.eventBus().<String>consumer("news-feed") .bodyStream() .toObservable(); // Send the event to the client Subscription subscription = msg.subscribe(sockJSSocket::write); // Unsubscribe when the socket closes sockJSSocket.endHandler(v -> { subscription.unsubscribe(); }); })); // Serve the static resources router.route().handler(StaticHandler.create()); vertx.createHttpServer().requestHandler(router).listen(8080); // Publish a message to the address "news-feed" every second vertx.setPeriodic(1000, t -> vertx.eventBus().publish("news-feed", "news from the server!")); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer().requestHandler(routerProvider.get()::accept); server.rxListen(httpServerPort).subscribe(); } }
@Override public void start(Future<Void> startFuture) { // deploy the web server HttpServerOptions options = new HttpServerOptions() .setCompressionSupported(true); HttpServer server = vertx.createHttpServer(options); server.requestHandler(this::onRequest); server.listenObservable(8080) // listen on port 8080 .subscribe(v -> startFuture.complete(), startFuture::fail); }
protected Single<Void> createHttpServer(Router router, String host, int port) { return vertx.createHttpServer() .requestHandler(router::accept) .rxListen(port, host) .map(r -> null); }
@Override public void start(Future<Void> startFuture) throws Exception { String name = this.getClass().getSimpleName(); int port = eventBusBridgeProperties.getPort(); String routeRegex = eventBusBridgeProperties.getRoutePrefix() .map(val -> String.format("/%s/*", val)) .orElse("/*"); log.info("Starting {} on port {} with route {}...", name, port, routeRegex); Router router = Router.router(vertx); router.route(routeRegex).handler(sockJSHandler); vertx.createHttpServer() .requestHandler(router::accept) .rxListen(port) .subscribe(server -> { log.info("Started {} on port {}", name, server.actualPort()); startFuture.complete(); }, startFuture::fail); }
@Test public void testObserverToFuture() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)).requestHandler(req -> {}); AtomicInteger count = new AtomicInteger(); Observer<HttpServer> observer = new Observer<HttpServer>() { @Override public void onCompleted() { server.close(); assertEquals(1, count.get()); testComplete(); } @Override public void onError(Throwable e) { fail(e.getMessage()); } @Override public void onNext(HttpServer httpServer) { count.incrementAndGet(); } }; Single<HttpServer> onListen = server.rxListen(); onListen.subscribe(observer); await(); }
@Override public void start() throws Exception { HttpServer s1 = vertx.createHttpServer(new HttpServerOptions().setPort(8080)).requestHandler(req -> { }); HttpServer s2 = vertx.createHttpServer(new HttpServerOptions().setPort(8081)).requestHandler(req -> { }); Single<HttpServer> f1 = s1.rxListen(); Single<HttpServer> f2 = s2.rxListen(); Action1<HttpServer> done = server -> { if (count.incrementAndGet() == 2) { testComplete(); } }; f1.subscribe(done); f2.subscribe(done); } });
@Test public void testWebsocketClientFlatMap() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.websocketStream().handler(ws -> { ws.write(Buffer.buffer("some_content")); ws.close(); }); server.listen(ar -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); Buffer content = Buffer.buffer(); client. websocketStream(8080, "localhost", "/the_uri"). toObservable(). flatMap(WebSocket::toObservable). forEach(content::appendBuffer, err -> fail(), () -> { server.close(); assertEquals("some_content", content.toString("UTF-8")); testComplete(); }); }); await(); }
@Test public void testWebsocketClient() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.websocketStream().handler(ws -> { ws.write(Buffer.buffer("some_content")); ws.close(); }); server.listen(ar -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); client.websocket(8080, "localhost", "/the_uri", ws -> { Buffer content = Buffer.buffer(); Observable<Buffer> observable = ws.toObservable(); observable.forEach(content::appendBuffer, err -> fail(), () -> { server.close(); assertEquals("some_content", content.toString("UTF-8")); testComplete(); }); }); }); await(); }
@Test public void testHttpClientFlatMap() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> { req.response().setChunked(true).end("some_content"); }); server.listen(ar -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); Single<HttpClientResponse> req = client.rxGetNow(8080, "localhost", "/the_uri"); Buffer content = Buffer.buffer(); req.flatMapObservable(HttpClientResponse::toObservable).forEach( content::appendBuffer, err -> fail(), () -> { server.close(); assertEquals("some_content", content.toString("UTF-8")); testComplete(); }); }); await(); }
@Test public void testHttpClient() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> { req.response().setChunked(true).end("some_content"); }); try { server.listen(ar -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); client.rxGetNow(8080, "localhost", "/the_uri").subscribe(resp -> { Buffer content = Buffer.buffer(); Observable<Buffer> observable = resp.toObservable(); observable.forEach(content::appendBuffer, err -> fail(), () -> { assertEquals("some_content", content.toString("UTF-8")); testComplete(); }); }); }); await(); } finally { server.close(); } }