@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!")); } }
private void configureStaticHandler(VertxFactory vertxFactory, Router router) { if (vertxFactory.router().isStaticHandlerDefined()) { Route staticRoute; if (vertxFactory.router().staticHandler().path() != null) { staticRoute = router.route(vertxFactory.router().staticHandler().path()); } else { staticRoute = router.route(); } staticRoute.handler(vertxFactory.router().staticHandler().root() != null ? StaticHandler.create(vertxFactory.router().staticHandler().root()) : StaticHandler.create()); } } }