Vert.x活动巴士消费者两次
我编写了一个简单的代码,用于在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论