2016-11-20 66 views
1

我想用Vert.x 3.3.2和socketJS与eventbus代理一次性忘掉URL,但只是在消息中交谈而不创建REST接口 - 最好的事多年来在Java平台上!然而,我的你好世界不工作。通过事件总线的Java Vert.x和SockJS

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

爪哇:

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

    } 

} 

在文件夹中的文档/ 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> 

响应如下:

首先浏览器客户端被加载: SERVER:

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) 

CLIENT 2:

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

有什么不对?

回答

0

你的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>

+0

TNX!然而,它看起来像你使用旧代码。在 文档[link](http://vertx.io/docs/vertx-web/java/#_sockjs_event_bus_bridge)中查看示例中的'registerHandler'调用,并在[link]中检查从3.0.x到3.1的Changelog (https://www.npmjs.com/package/vertx3-eventbus-client)。但是,js可能确实老了,我会尝试从CDN中获取它。 – Niels

+0

github [link](https://github.com/vert-x3/vertx-bus-bower)看起来像最新的源代码,而不是npmjs – Niels