简体   繁体   English

Java Vert.x和SockJS通过Eventbus

[英]Java Vert.x and SockJS through Eventbus

I would like to use Vert.x 3.3.2 and socketJS with the eventbus proxy to forget about URL's once and for all but just to talk in messages without creating REST interfaces -best thing in years on the Java platform!. 我想将Vert.x 3.3.2和socketJS与eventbus代理一起使用,忘记了URL的一劳永逸,但只是在没有创建REST接口的情况下在消息中进行交谈 - 多年来在Java平台上最好的事情! However, my hello world is not working. 但是,我的你好世界没有用。

My test is as follows: on every connected and disconnected event clientside and serverside, it is printed at the console. 我的测试如下:在每个连接和断开连接的事件客户端和服务器端,它在控制台上打印。 And, when a client is connected, the client sends a message on which the server respons, and the server sends a notification to everyone too. 并且,当客户端连接时,客户端发送服务器响应的消息,服务器也向每个人发送通知。 What happens is that only the first client sends its message and gets proper response from the server. 会发生什么是只有第一个客户端发送其消息并从服务器获得适当的响应。

Java: 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");
    });

    }

}

In folder doc/clientSocketJS there is a index.html: 在文件夹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>

Responses are as follows: 回应如下:

First browser client is loaded: SERVER: 加载第一个浏览器客户端:SERVER:

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

CLIENT 1: 客户1:

connected
response: I received so I reply

This is as expected. 这是预期的。 Then I load a second client (same or other browser): 然后我加载第二个客户端(相同或其他浏览器):

SERVER 服务器

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

CLIENT 2: 客户2:

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

What's wrong? 怎么了?

You Java code seems totally fine. 你的Java代码似乎完全没问题。 But JavaScript code looks like it was taken from an old example. 但JavaScript代码看起来就像是从一个旧例子中获取的。
First parameter of registerHandler callback is the message itself, not error. registerHandler回调的第一个参数是消息本身,而不是错误。
I also used hosted VertxEventBus, instead of the provided, just in case. 我还使用了托管的VertxEventBus,而不是提供的,以防万一。

This should work: 这应该工作:

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM