Search in sources :

Example 1 with BridgeOptions

use of io.vertx.ext.web.handler.sockjs.BridgeOptions 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 BridgeOptions

use of io.vertx.ext.web.handler.sockjs.BridgeOptions 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 BridgeOptions

use of io.vertx.ext.web.handler.sockjs.BridgeOptions 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 BridgeOptions

use of io.vertx.ext.web.handler.sockjs.BridgeOptions 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 BridgeOptions

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

the class WebExamples method example46.

public void example46(Vertx vertx) {
    Router router = Router.router(vertx);
    SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
    // Let through any messages sent to 'demo.orderMgr' from the client
    PermittedOptions inboundPermitted1 = new PermittedOptions().setAddress("demo.orderMgr");
    // Allow calls to the address 'demo.persistor' from the client as long as the messages
    // have an action field with value 'find' and a collection field with value
    // 'albums'
    PermittedOptions inboundPermitted2 = new PermittedOptions().setAddress("demo.persistor").setMatch(new JsonObject().put("action", "find").put("collection", "albums"));
    // Allow through any message with a field `wibble` with value `foo`.
    PermittedOptions inboundPermitted3 = new PermittedOptions().setMatch(new JsonObject().put("wibble", "foo"));
    // First let's define what we're going to allow from server -> client
    // Let through any messages coming from address 'ticker.mystock'
    PermittedOptions outboundPermitted1 = new PermittedOptions().setAddress("ticker.mystock");
    // Let through any messages from addresses starting with "news." (e.g. news.europe, news.usa, etc)
    PermittedOptions outboundPermitted2 = new PermittedOptions().setAddressRegex("news\\..+");
    // Let's define what we're going to allow from client -> server
    BridgeOptions options = new BridgeOptions().addInboundPermitted(inboundPermitted1).addInboundPermitted(inboundPermitted1).addInboundPermitted(inboundPermitted3).addOutboundPermitted(outboundPermitted1).addOutboundPermitted(outboundPermitted2);
    sockJSHandler.bridge(options);
    router.route("/eventbus/*").handler(sockJSHandler);
}
Also used : JsonObject(io.vertx.core.json.JsonObject) BridgeOptions(io.vertx.ext.web.handler.sockjs.BridgeOptions) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) SockJSHandler(io.vertx.ext.web.handler.sockjs.SockJSHandler)

Aggregations

BridgeOptions (io.vertx.ext.web.handler.sockjs.BridgeOptions)20 SockJSHandler (io.vertx.ext.web.handler.sockjs.SockJSHandler)14 Router (io.vertx.ext.web.Router)12 PermittedOptions (io.vertx.ext.web.handler.sockjs.PermittedOptions)12 JsonObject (io.vertx.core.json.JsonObject)9 PermittedOptions (io.vertx.ext.bridge.PermittedOptions)6 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