首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vt.x SockJSHandler类

Vt.x SockJSHandler类
EN

Stack Overflow用户
提问于 2019-11-19 18:08:08
回答 2查看 575关注 0票数 1

我正在尝试用Vert.x构建一个web应用程序,我已经找到了许多使用SockJSHandler与应用程序前端的SockJS javascript库进行通信的示例。

示例使用版本3.5.0 of Vert.x CoreVert.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实例路由到特定路由的最佳方式是什么?

EN

回答 2

Stack Overflow用户

发布于 2020-01-11 16:33:30

见A.

代码语言:javascript
复制
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));
代码语言:javascript
复制
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());

为我工作的代码:

代码语言:javascript
复制
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));
票数 1
EN

Stack Overflow用户

发布于 2019-11-19 20:15:46

我已经在3.8.2版本的降级文档中找到了我需要的文档。

处理SockJSHandler.bridge返回值的正确方法是将其包含为子路由器。

因此,与其使用类似于(preVert.xweb 3.8.2版本)的实现,不如:

代码语言:javascript
复制
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+):

代码语言:javascript
复制
EventBus eventBus = vertx.eventBus();
Handler<BridgeEvent> handler = ...;
Router sockJSSubRouter = SockJSHandler.create(vertx).bridge(options, handler);
Router router = Router.router(vertx);
router.mountSubRouter("/eventbusroute", sockJSSubRouter);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58940327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档