Vert.x活动巴士消费者两次

发布于 2025-01-19 18:12:38 字数 1512 浏览 5 评论 0原文

我编写了一个简单的代码,用于在 vert.x 中的 java 服务器和具有事件总线库的浏览器客户端之间进行通信。客户端通过事件总线向服务器发送消息,服务器调用 eventbus.consumer 读取消息并回复。 我注意到一些奇怪的事情,我不知道为什么消费者方法被调用两次,我认为它调用了一次。我该如何解决这个问题?谢谢。 这是我的代码:

服务器

 SockJSBridgeOptions options = new SockJSBridgeOptions().
      addInboundPermitted(new PermittedOptions().setAddressRegex("out"));

    sockJSHandler.bridge(options);

    router.route("/eventbus/*").handler(sockJSHandler);

    eventBus.<String>consumer("out", event -> {
      logger.info(event.body());
      event.reply("TEST");
    });

    vertx.createHttpServer().requestHandler(router).listen(serverPort, res -> {
      if (res.succeeded()) {
        startPromise.complete();
      } else {
        startPromise.fail(res.cause());
      }
    });

客户端

  var eb = new EventBus('http://localhost:8088/eventbus');
  eb.onopen = () => {    
    eb.send('out', {name: 'tim', age: 5817}, (e, m) => {
      console.log("TEST RESPONSE")
      console.log(JSON.stringify(m));
    });
   }

控制台服务器输出

apr 07, 2022 7:55:43 PM it.unibo.guessthesong.lobby.LobbyVerticle
INFO: {"name":"tim","age":5817}
apr 07, 2022 7:55:43 PM it.unibo.guessthesong.lobby.LobbyVerticle
INFO: {"name":"tim","age":5817}

控制台客户端输出

TEST RESPONSE
{"type":"rec","address":"92888ada-fada-43ff-9677-9eeed95b44da","body":"TEST"}
TEST RESPONSE
{"type":"rec","address":"2ced1214-13cf-4ebf-b7d8-6773612097e7","body":"TEST"}

i wrote a simple code for communicate between a java server in vert.x and a browser client with the event bus library. The client send a message to the server through the event bus and the server call eventbus.consumer to read the message and reply to it.
I notice something strange, idk why the consumer method is called twice, I aspect it call one. How can i solve this? Thanks.
This is my code:

SERVER

 SockJSBridgeOptions options = new SockJSBridgeOptions().
      addInboundPermitted(new PermittedOptions().setAddressRegex("out"));

    sockJSHandler.bridge(options);

    router.route("/eventbus/*").handler(sockJSHandler);

    eventBus.<String>consumer("out", event -> {
      logger.info(event.body());
      event.reply("TEST");
    });

    vertx.createHttpServer().requestHandler(router).listen(serverPort, res -> {
      if (res.succeeded()) {
        startPromise.complete();
      } else {
        startPromise.fail(res.cause());
      }
    });

CLIENT

  var eb = new EventBus('http://localhost:8088/eventbus');
  eb.onopen = () => {    
    eb.send('out', {name: 'tim', age: 5817}, (e, m) => {
      console.log("TEST RESPONSE")
      console.log(JSON.stringify(m));
    });
   }

CONSOLE SERVER OUTPUT

apr 07, 2022 7:55:43 PM it.unibo.guessthesong.lobby.LobbyVerticle
INFO: {"name":"tim","age":5817}
apr 07, 2022 7:55:43 PM it.unibo.guessthesong.lobby.LobbyVerticle
INFO: {"name":"tim","age":5817}

CONSOLE CLIENT OUTPUT

TEST RESPONSE
{"type":"rec","address":"92888ada-fada-43ff-9677-9eeed95b44da","body":"TEST"}
TEST RESPONSE
{"type":"rec","address":"2ced1214-13cf-4ebf-b7d8-6773612097e7","body":"TEST"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文