use of io.vertx.ext.web.handler.sockjs.BridgeOptions 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);
}
use of io.vertx.ext.web.handler.sockjs.BridgeOptions in project vertx-web by vert-x3.
the class WebExamples method example47.
public void example47() {
// Let through any messages sent to 'demo.orderService' from the client
PermittedOptions inboundPermitted = new PermittedOptions().setAddress("demo.orderService");
// But only if the user is logged in and has the authority "place_orders"
inboundPermitted.setRequiredAuthority("place_orders");
BridgeOptions options = new BridgeOptions().addInboundPermitted(inboundPermitted);
}
use of io.vertx.ext.web.handler.sockjs.BridgeOptions 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");
}
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 outbound traffic to the news-feed address
BridgeOptions options = new BridgeOptions().addOutboundPermitted(new PermittedOptions().setAddress("news-feed"));
router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(options, event -> {
if (event.type() == BridgeEventType.SOCKET_CREATED) {
System.out.println("A socket was created");
}
// This signals that it's ok to process the event
event.complete(true);
}));
// Serve the static resources
router.route().handler(StaticHandler.create());
vertx.createHttpServer().requestHandler(router::accept).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!"));
}
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 {
// Create a mongo client using all defaults (connect to localhost and default port) using the database name "demo".
mongo = MongoClient.createShared(vertx, new JsonObject().put("db_name", "demo"));
// the load function just populates some data on the storage
loadData(mongo);
// the app works 100% realtime
vertx.eventBus().consumer("vtoons.listAlbums", this::listAlbums);
vertx.eventBus().consumer("vtoons.placeOrder", this::placeOrder);
Router router = Router.router(vertx);
// We need cookies and sessions
router.route().handler(CookieHandler.create());
router.route().handler(BodyHandler.create());
router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));
// Simple auth service which uses a properties file for user/role info
AuthProvider authProvider = ShiroAuth.create(vertx, ShiroAuthRealmType.PROPERTIES, new JsonObject());
// We need a user session handler too to make sure the user is stored in the session between requests
router.route().handler(UserSessionHandler.create(authProvider));
router.post("/login").handler(ctx -> {
JsonObject credentials = ctx.getBodyAsJson();
if (credentials == null) {
// bad request
ctx.fail(400);
return;
}
// use the auth handler to perform the authentication for us
authProvider.authenticate(credentials, login -> {
// error handling
if (login.failed()) {
// forbidden
ctx.fail(403);
return;
}
ctx.setUser(login.result());
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json").end("{}");
});
});
router.route("/eventbus/*").handler(ctx -> {
// we need to be logged in
if (ctx.user() == null) {
ctx.fail(403);
} else {
ctx.next();
}
});
// Allow outbound traffic to the vtoons addresses
BridgeOptions options = new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("vtoons.listAlbums")).addInboundPermitted(new PermittedOptions().setAddress("vtoons.login")).addInboundPermitted(new PermittedOptions().setAddress("vtoons.placeOrder").setRequiredAuthority("place_order")).addOutboundPermitted(new PermittedOptions());
router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(options));
// Serve the static resources
router.route().handler(StaticHandler.create());
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
Aggregations