【问题标题】:Java Vert.x and SockJS through Eventbus通过 Eventbus 实现 Java Vert.x 和 SockJS
【发布时间】:2016-11-20 18:35:28
【问题描述】:

我想将 Vert.x 3.3.2 和 socketJS 与 eventbus 代理一起使用,以便一劳永逸地忘记 URL,而只是在不创建 REST 接口的情况下在消息中交谈——Java 平台上多年来最好的事情!但是,我的 hello world 无法正常工作。

我的测试如下:在每个连接和断开的事件客户端和服务器端,都在控制台打印。而且,当客户端连接时,客户端会发送一条消息,服务器会对此做出响应,服务器也会向所有人发送通知。发生的情况是只有第一个客户端发送它的消息并从服务器获得正确的响应。

Java:

public class App extends AbstractVerticle {

public static void main(String[] args) throws InterruptedException {
    Vertx.vertx().deployVerticle(MethodHandles.lookup().lookupClass().getName());
}

@Override
public void start() throws IOException {
    Router router = Router.router(vertx);
    BridgeOptions options = new BridgeOptions();
    PermittedOptions address = new PermittedOptions().setAddress("some-address");
    options.addInboundPermitted(address);
    options.addOutboundPermitted(address);
    SockJSHandler sockJSHandler = SockJSHandler.create(vertx).bridge(options, be -> {
        if (be.type() == BridgeEventType.REGISTER) {
            System.out.println("sockJs: connected");
            vertx.eventBus().publish("some-address", "hey all, we have a new subscriber ");
        }
        be.complete(true);
    });
    router.route("/sockjs/*").handler(sockJSHandler);
    router.route("/web/*").handler(StaticHandler.create("doc/clientSocketJS"));
    vertx.createHttpServer().requestHandler(router::accept).listen(8020, listenHandler -> {
        if (listenHandler.failed()) {
            LOGGER.log(Level.SEVERE, "Startup error", listenHandler.cause());
            System.exit(0); // stop on startup error
        }
    });
    vertx.eventBus().consumer("some-address", message -> {
        System.out.println("sockJs: received: " + message.body());
        message.reply("I received so I reply");
    });

    }

}

在文件夹 doc/clientSocketJS 中有一个 index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js"></script>
<script src="js/vertx-eventbus.js"></script>
<script>
    var eb = new EventBus('http://localhost:8020/sockjs');
    eb.onopen = function() {
        console.log('connected');
        eb.registerHandler('some-address', function(error, message) {
            console.log('received: ' + JSON.stringify(message));
        });
        eb.send('some-address', {
            name : 'from ' + navigator.product
        }, null, function(a, message) {
            if (message == null) {
                console.log("ERROR: response null");
            } else {
            console.log('response: ', message.body);
            }
        });
    }
    eb.onclose = function() {
        console.log("disconnected");
        eb = null;
    };
</script>
</head>
</html>

回复如下:

第一个浏览器客户端被加载: 服务器:

sockJs: connected
sockJs: received: hey all, we have a new subscriber 
sockJs: received: {"name":"from Gecko"}

客户 1:

connected
response: I received so I reply

这符合预期。然后我加载第二个客户端(相同或其他浏览器):

服务器

sockJs: connected
sockJs: received: hey all, we have a new subscriber 
(expecting a 'name' just like first time, but it doesn't appear)

客户 2:

connected
(after a while) ERROR: response null  @ index.html::18

怎么了?

【问题讨论】:

    标签: java vert.x sockjs


    【解决方案1】:

    您的 Java 代码似乎完全没问题。但是 JavaScript 代码看起来像是取自一个旧示例。
    registerHandler 回调的第一个参数是消息本身,而不是错误。
    我还使用了托管的 VertxEventBus,而不是提供的,以防万一。

    这应该可行:

    var eb = new vertx.EventBus('http://localhost:8020/sockjs');
            eb.onopen = function() {
                console.log('connected');
                eb.registerHandler('some-address', function(message) {
                    console.log('received: ' + JSON.stringify(message));
                });
                eb.send('some-address', {
                    name : 'from ' + navigator.product
                }, null, function(a, message) {
                    if (message == null) {
                        console.log("ERROR: response null");
                    } else {
                        console.log('response: ', message.body);
                    }
                });
            };
            eb.onclose = function() {
                console.log("disconnected");
                eb = null;
            };
        <script src="http://cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/vertx/2.0.0/vertxbus.min.js"></script>

    【讨论】:

    • tnx!但是,看起来您使用的是旧代码。在文档link 的示例中检查“registerHandler”调用,并在link 检查从 3.0.x 到 3.1 的变更日志。不过js可能确实老了,我会尝试从CDN获取。
    • github link 看起来像是最新的源代码,而不是 npmjs
    猜你喜欢
    • 2017-04-05
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    相关资源
    最近更新 更多