Search in sources :

Example 1 with SockJSHandler

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

Example 2 with SockJSHandler

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

the class WebExamples method example44.

public void example44(Vertx vertx) {
    Router router = Router.router(vertx);
    SockJSHandlerOptions options = new SockJSHandlerOptions().setHeartbeatInterval(2000);
    SockJSHandler sockJSHandler = SockJSHandler.create(vertx, options);
    sockJSHandler.socketHandler(sockJSSocket -> {
        // Just echo the data back
        sockJSSocket.handler(sockJSSocket::write);
    });
    router.route("/myapp/*").handler(sockJSHandler);
}
Also used : SockJSHandlerOptions(io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions) SockJSHandler(io.vertx.ext.web.handler.sockjs.SockJSHandler)

Example 3 with SockJSHandler

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

the class WebExamples method example45.

public void example45(Vertx vertx) {
    Router router = Router.router(vertx);
    SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
    BridgeOptions options = new BridgeOptions();
    sockJSHandler.bridge(options);
    router.route("/eventbus/*").handler(sockJSHandler);
}
Also used : BridgeOptions(io.vertx.ext.web.handler.sockjs.BridgeOptions) SockJSHandler(io.vertx.ext.web.handler.sockjs.SockJSHandler)

Example 4 with SockJSHandler

use of io.vertx.ext.web.handler.sockjs.SockJSHandler 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);
    ebHandler.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).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 5 with SockJSHandler

use of io.vertx.ext.web.handler.sockjs.SockJSHandler 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);
    ebHandler.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).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)

Aggregations

SockJSHandler (io.vertx.ext.web.handler.sockjs.SockJSHandler)16 BridgeOptions (io.vertx.ext.web.handler.sockjs.BridgeOptions)14 Router (io.vertx.ext.web.Router)8 PermittedOptions (io.vertx.ext.web.handler.sockjs.PermittedOptions)8 JsonObject (io.vertx.core.json.JsonObject)7 EventBus (io.vertx.core.eventbus.EventBus)5 PermittedOptions (io.vertx.ext.bridge.PermittedOptions)4 SockJSHandlerOptions (io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions)3 AuthProvider (io.vertx.ext.auth.AuthProvider)1 Route (io.vertx.ext.web.Route)1