我正在尝试用Vert.x构建一个web应用程序,我已经找到了许多使用SockJSHandler与应用程序前端的SockJS javascript库进行通信的示例。
示例使用版本3.5.0 of Vert.x Core和Vert.x Web,并使用方法SockJSHandler.create(vertx)创建类的实例,通过方法SockJSHandler.bridge将SockJSHandler桥接到Vert.x事件总线。最后一个方法返回SockJSHandler类型,该类型使用router.route("/eventbusroute/*").handler(sockJsHandler())路由。
我使用的是Vert.xCore和Vert.xWeb的最新版本3.8.3,在这个版本中,bridge返回一个Router类型的实例,如文档中所述。
我的问题是:将SockJSHandler Router实例路由到特定路由的最佳方式是什么?
发布于 2020-01-11 16:33:30
见A.
Router router = Router.router(vertx);
SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
BridgeOptions options = new BridgeOptions();
// mount the bridge on the router
router.mountSubRouter("/eventbus", sockJSHandler.bridge(options));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.
router.mountSubRouter("/eventbus", SockJSHandler.create(vertx).bridge(opts));
router.route().handler(StaticHandler.create());为我工作的代码:
Router router = Router.router(vertx);
BridgeOptions options = new BridgeOptions();
options.addOutboundPermitted(new PermittedOptions().setAddressRegex(".*"));
options.addInboundPermitted(new PermittedOptions().setAddressRegex(".*"));
SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
router.mountSubRouter("/eventbus", sockJSHandler.bridge(options));发布于 2019-11-19 20:15:46
我已经在3.8.2版本的降级文档中找到了我需要的文档。
处理SockJSHandler.bridge返回值的正确方法是将其包含为子路由器。
因此,与其使用类似于(preVert.xweb 3.8.2版本)的实现,不如:
EventBus eventBus = vertx.eventBus();
Handler<BridgeEvent> handler = ...;
SockJSHandler sockJSHandler = SockJSHandler.create(vertx).bridge(options, handler);
Router router = Router.router(vertx);
router.route("/eventbusroute/*").handler(sockJSHandler);必须将bridge方法调用的结果包含为如下subRouter (Vert.xweb版本3.8.2+):
EventBus eventBus = vertx.eventBus();
Handler<BridgeEvent> handler = ...;
Router sockJSSubRouter = SockJSHandler.create(vertx).bridge(options, handler);
Router router = Router.router(vertx);
router.mountSubRouter("/eventbusroute", sockJSSubRouter);https://stackoverflow.com/questions/58940327
复制相似问题