@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).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)); }); } }
@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); // 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(); 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)); }); } }
@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); // 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(); 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)); }); } }
@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("com.example:cmd:poke-server")) .addOutboundPermitted(new PermittedOptions().setAddress("com.example:stat:server-info")); // 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).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("com.example:stat:server-info", new JsonObject().put("systemTime", timestamp)); }); } }
BridgeOptions options = new BridgeOptions(); options.setOutboundPermitted(Collections.singletonList(new PermittedOptions().setAddress("dashboard"))); router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(options));
@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).listen(8080); } }
BridgeOptions opts = new BridgeOptions() .addInboundPermitted(new PermittedOptions().setAddress("chat.to.server")) .addOutboundPermitted(new PermittedOptions().setAddress("chat.to.client"));
@Override public void start() throws Exception { // Create the client object MyService service = new MyServiceImpl(); // Register the handler new ServiceBinder(vertx) .setAddress("proxy.example") .register(MyService.class, service); Router router = Router.router(vertx); BridgeOptions options = new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("proxy.example")); router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(options)); // Serve the static resources router.route().handler(StaticHandler.create()); vertx.createHttpServer().requestHandler(router).listen(8080); } }
BridgeOptions options = new BridgeOptions().addOutboundPermitted(new PermittedOptions().setAddress("news-feed"));
@Override public void start() throws Exception { // Create the client object ProcessorService service = new ProcessorServiceImpl(); // Register the handler new ServiceBinder(vertx) .setAddress("vertx.processor") .register(ProcessorService.class, service); 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("vertx.processor")) .addOutboundPermitted(new PermittedOptions().setAddress("vertx.processor")); // Create the event bus bridge and add it to the router. SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts); router.route("/eventbus/*").handler(ebHandler); router.route().handler(StaticHandler.create()); // vertx.createHttpServer().requestHandler(router).listen(8080); }
BridgeOptions options = new BridgeOptions(). addOutboundPermitted( new PermittedOptions().
BridgeOptions options = new BridgeOptions()
@Test public void testMaxAddressLength() throws Exception { CountDownLatch latch = new CountDownLatch(1); sockJSHandler.bridge(new BridgeOptions(allAccessOptions).setMaxAddressLength(10)); client.websocket(websocketURI, ws -> { JsonObject msg = new JsonObject().put("type", "register").put("address", "someaddressyqgyuqwdyudyug"); ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(msg.encode(), true)); ws.handler(buff -> { String str = buff.toString(); JsonObject received = new JsonObject(str); assertEquals("err", received.getString("type")); assertEquals("max_address_length_reached", received.getString("body")); latch.countDown(); }); }); awaitLatch(latch); }
sockJSHandler.bridge(new BridgeOptions(allAccessOptions).setMaxHandlersPerSocket(maxHandlers));
@Test public void testInvalidMessageCode() { router.route("/ws-timeout/*").handler(SockJSHandler .create(vertx) .bridge(new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("SockJSHandlerTest.testInvalidMessageCode"))) ); vertx.eventBus().consumer("SockJSHandlerTest.testInvalidMessageCode", msg -> msg.reply(new JsonObject())); client.websocket("/ws-timeout/websocket", ws -> { ws.writeFinalBinaryFrame(Buffer.buffer("durp!")); ws.frameHandler(frame -> { // we should get a normal frame with a error message if (!frame.isClose()) { JsonObject msg = new JsonObject(frame.binaryData()); assertEquals("err", msg.getString("type")); assertEquals("invalid_json", msg.getString("body")); testComplete(); ws.close(); } }); }); await(); } }
@Override public void start() throws Exception { /* test service */ final TestService testService = new TestService(); /* address */ final String address = "testservice"; /* service builder */ final ServiceBuilder serviceBuilder = ServiceBuilder.serviceBuilder(); serviceBuilder.setServiceObject(testService); final ServiceQueue serviceQueue = serviceBuilder.build(); /* vertx event bus bridge to qbit. */ final VertxEventBusBridgeBuilder vertxEventBusBridgeBuilder = VertxEventBusBridgeBuilder .vertxEventBusBridgeBuilder() .setVertx(vertx); vertxEventBusBridgeBuilder.addBridgeAddress(address, TestService.class); final Router router = Router.router(vertx); router.route("/health/").handler(routingContext -> routingContext.response().end("\"ok\"")); /* Configure bridge at this HTTP/WebSocket URI. */ router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge( new BridgeOptions() .addInboundPermitted(new PermittedOptions().setAddress(address)) .addOutboundPermitted(new PermittedOptions().setAddress(address)) )); vertxEventBusBridgeBuilder.setServiceQueue(serviceQueue); serviceQueue.startAll(); //startall not supported yet for bridge. vertxEventBusBridgeBuilder.build(); vertx.createHttpServer().requestHandler(router::accept).listen(8080); System.out.println("Bound to 8080"); }
@Test public void testTimeoutCloseCode() { router.route("/ws-timeout/*").handler(SockJSHandler .create(vertx) .bridge(new BridgeOptions().setPingTimeout(1)) ); client.websocket("/ws-timeout/websocket", ws -> ws.frameHandler(frame -> { if (frame.isClose()) { assertEquals(1001, frame.closeStatusCode()); assertEquals("Session expired", frame.closeReason()); testComplete(); } })); await(); }
public static io.vertx.ext.web.handler.sockjs.SockJSHandler bridge(io.vertx.ext.web.handler.sockjs.SockJSHandler j_receiver, java.util.Map<String, Object> bridgeOptions, io.vertx.core.Handler<io.vertx.ext.web.handler.sockjs.BridgeEvent> bridgeEventHandler) { io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.bridge(bridgeOptions != null ? new io.vertx.ext.web.handler.sockjs.BridgeOptions(io.vertx.core.impl.ConversionHelper.toJsonObject(bridgeOptions)) : null, bridgeEventHandler != null ? event -> bridgeEventHandler.handle(io.vertx.core.impl.ConversionHelper.fromObject(event)) : null)); return j_receiver; } }
private BridgeOptions configureBridgeOptions(JsonObject config) { BridgeOptions bridgeOptions = new BridgeOptions(); JsonObject addresses=config.getJsonObject(ADDRESSES); JsonArray outbounds=addresses.getJsonArray(OUTBOUND); JsonArray inbounds=addresses.getJsonArray(INBOUND); outbounds.forEach(o-> addOutboundPermitted(bridgeOptions, (JsonObject) o)); inbounds.forEach(i-> addInboundPermitted(bridgeOptions, (JsonObject) i)); return bridgeOptions; }
@Override public SockJSHandler configure(VertxContext vertxContext, ServerConfiguration<JsonObject, JsonObject[]> config) { BridgeOptions bridgeOptions=new BridgeOptions(); bridgeOptions.addOutboundPermitted(new PermittedOptions().setAddress(VertxBusContext.DEFAULT_SOCKJS_ADDRESS)); SockJSHandler sockJSHandler = SockJSHandler.create(vertxContext.vertx()); sockJSHandler.bridge(bridgeOptions, event -> event.complete(true)); sockJSHandler.socketHandler(event -> event.headers().set(ACCESS_CONTROL_ALLOW_ORIGIN, ALLOW_ALL)); return sockJSHandler; } }