Search in sources :

Example 1 with PermittedOptions

use of io.vertx.ext.web.handler.sockjs.PermittedOptions in project vertx-examples by vert-x3.

the class DashboardVerticle method start.

@Override
public void start() throws Exception {
    Router router = Router.router(vertx);
    // The event bus bridge handler
    BridgeOptions options = new BridgeOptions();
    options.setOutboundPermitted(Collections.singletonList(new PermittedOptions().setAddress("dashboard")));
    router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(options));
    // The web server handler
    router.route().handler(StaticHandler.create().setCachingEnabled(false));
    // Start http server
    HttpServer httpServer = vertx.createHttpServer();
    httpServer.requestHandler(router::accept).listen(8080, ar -> {
        if (ar.succeeded()) {
            System.out.println("Http server started");
        } else {
            ar.cause().printStackTrace();
        }
    });
    // Our dashboard that aggregates metrics from various kafka topics
    JsonObject dashboard = new JsonObject();
    // Publish the dashboard to the browser over the bus
    vertx.setPeriodic(1000, timerID -> {
        vertx.eventBus().publish("dashboard", dashboard);
    });
    // Get the Kafka consumer config
    JsonObject config = config();
    // Create the consumer
    KafkaReadStream<String, JsonObject> consumer = KafkaReadStream.create(vertx, config.getMap(), String.class, JsonObject.class);
    // Aggregates metrics in the dashboard
    consumer.handler(record -> {
        JsonObject obj = record.value();
        dashboard.mergeIn(obj);
    });
    // Subscribe to Kafka
    consumer.subscribe(Collections.singleton("the_topic"));
}
Also used : HttpServer(io.vertx.core.http.HttpServer) Router(io.vertx.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) BridgeOptions(io.vertx.ext.web.handler.sockjs.BridgeOptions) PermittedOptions(io.vertx.ext.web.handler.sockjs.PermittedOptions)

Example 2 with PermittedOptions

use of io.vertx.ext.web.handler.sockjs.PermittedOptions in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    Router router = Router.router(vertx);
    // Allow events for the designated addresses in/out of the event bus bridge
    BridgeOptions opts = new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("chat.message")).addOutboundPermitted(new PermittedOptions().setAddress("chat.message"));
    // Create the event bus bridge and add it to the router.
    SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
    router.route("/eventbus/*").handler(ebHandler);
    // Create a router endpoint for the static content.
    router.route().handler(StaticHandler.create());
    // Start the web server and tell it to use the router to handle requests.
    vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
Also used : Router(io.vertx.ext.web.Router) BridgeOptions(io.vertx.ext.web.handler.sockjs.BridgeOptions) PermittedOptions(io.vertx.ext.web.handler.sockjs.PermittedOptions) SockJSHandler(io.vertx.ext.web.handler.sockjs.SockJSHandler)

Example 3 with PermittedOptions

use of io.vertx.ext.web.handler.sockjs.PermittedOptions in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    Router router = Router.router(vertx);
    // Allow events for the designated addresses in/out of the event bus bridge
    BridgeOptions opts = new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("chat.to.server")).addOutboundPermitted(new PermittedOptions().setAddress("chat.to.client"));
    // Create the event bus bridge and add it to the router.
    SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
    router.route("/eventbus/*").handler(ebHandler);
    // Create a router endpoint for the static content.
    router.route().handler(StaticHandler.create());
    // Start the web server and tell it to use the router to handle requests.
    vertx.createHttpServer().requestHandler(router::accept).listen(8080);
    EventBus eb = vertx.eventBus();
    // Register to listen for messages coming IN to the server
    eb.consumer("chat.to.server").handler(message -> {
        // Create a timestamp string
        String timestamp = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(Date.from(Instant.now()));
        // Send the message back out to all clients with the timestamp prepended.
        eb.publish("chat.to.client", timestamp + ": " + message.body());
    });
}
Also used : Router(io.vertx.ext.web.Router) BridgeOptions(io.vertx.ext.web.handler.sockjs.BridgeOptions) PermittedOptions(io.vertx.ext.web.handler.sockjs.PermittedOptions) EventBus(io.vertx.core.eventbus.EventBus) SockJSHandler(io.vertx.ext.web.handler.sockjs.SockJSHandler)

Example 4 with PermittedOptions

use of io.vertx.ext.web.handler.sockjs.PermittedOptions in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    Router router = Router.router(vertx);
    // Allow events for the designated addresses in/out of the event bus bridge
    BridgeOptions opts = new BridgeOptions().addOutboundPermitted(new PermittedOptions().setAddress("feed"));
    // Create the event bus bridge and add it to the router.
    SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
    router.route("/eventbus/*").handler(ebHandler);
    // Start the web server and tell it to use the router to handle requests.
    vertx.createHttpServer().requestHandler(router::accept).listen(8080);
    EventBus eb = vertx.eventBus();
    vertx.setPeriodic(1000l, t -> {
        // Create a timestamp string
        String timestamp = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(Date.from(Instant.now()));
        eb.send("feed", new JsonObject().put("now", timestamp));
    });
}
Also used : Router(io.vertx.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) BridgeOptions(io.vertx.ext.web.handler.sockjs.BridgeOptions) PermittedOptions(io.vertx.ext.web.handler.sockjs.PermittedOptions) EventBus(io.vertx.core.eventbus.EventBus) SockJSHandler(io.vertx.ext.web.handler.sockjs.SockJSHandler)

Example 5 with PermittedOptions

use of io.vertx.ext.web.handler.sockjs.PermittedOptions in project vertx-examples by vert-x3.

the class Dashboard method start.

@Override
public void start() {
    MetricsService service = MetricsService.create(vertx);
    Router router = Router.router(vertx);
    // Allow outbound traffic to the news-feed address
    BridgeOptions options = new BridgeOptions().addOutboundPermitted(new PermittedOptions().setAddress("metrics"));
    router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(options));
    // Serve the static resources
    router.route().handler(StaticHandler.create());
    HttpServer httpServer = vertx.createHttpServer();
    httpServer.requestHandler(router::accept).listen(8080);
    // Send a metrics events every second
    vertx.setPeriodic(1000, t -> {
        JsonObject metrics = service.getMetricsSnapshot(vertx.eventBus());
        vertx.eventBus().publish("metrics", metrics);
    });
    // Send some messages
    Random random = new Random();
    vertx.eventBus().consumer("whatever", msg -> {
        vertx.setTimer(10 + random.nextInt(50), id -> {
            vertx.eventBus().send("whatever", "hello");
        });
    });
    vertx.eventBus().send("whatever", "hello");
}
Also used : Random(java.util.Random) MetricsService(io.vertx.ext.dropwizard.MetricsService) HttpServer(io.vertx.core.http.HttpServer) Router(io.vertx.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) BridgeOptions(io.vertx.ext.web.handler.sockjs.BridgeOptions) PermittedOptions(io.vertx.ext.web.handler.sockjs.PermittedOptions)

Aggregations

BridgeOptions (io.vertx.ext.web.handler.sockjs.BridgeOptions)12 PermittedOptions (io.vertx.ext.web.handler.sockjs.PermittedOptions)12 Router (io.vertx.ext.web.Router)11 SockJSHandler (io.vertx.ext.web.handler.sockjs.SockJSHandler)8 JsonObject (io.vertx.core.json.JsonObject)7 EventBus (io.vertx.core.eventbus.EventBus)5 HttpServer (io.vertx.core.http.HttpServer)2 AbstractVerticle (io.vertx.core.AbstractVerticle)1 Runner (io.vertx.example.util.Runner)1 ProcessorServiceImpl (io.vertx.examples.service.impl.ProcessorServiceImpl)1 AuthProvider (io.vertx.ext.auth.AuthProvider)1 BridgeEventType (io.vertx.ext.bridge.BridgeEventType)1 MetricsService (io.vertx.ext.dropwizard.MetricsService)1 StaticHandler (io.vertx.ext.web.handler.StaticHandler)1 SockJSHandler (io.vertx.rxjava.ext.web.handler.sockjs.SockJSHandler)1 Random (java.util.Random)1