2016-11-20 3 views
1

Vertex 3.3.2とsocketJSをeventbusプロキシで使用して、一度URLを忘れてしまいましたが、RESTインターフェイスを作成せずにメッセージで話すことにしました。 Javaプラットフォーム上で!しかし、私のこんにちは世界は機能していません。Java Vert.xとSockJS〜Eventbus

私のテストは以下の通りです:接続されているか接続されていないすべてのイベントclientsideとserversideで、コンソールに表示されます。また、クライアントが接続されると、クライアントはサーバーが応答したメッセージを送信し、サーバーはすべてのユーザーに通知を送信します。何が起こるのは、最初のクライアントだけがメッセージを送信し、サーバーから適切な応答を得ることです。

のJava:フォルダのdoc/clientSocketJSで

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

    } 

} 

の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 

何が問題になっていますし、私は2番目のクライアント(同じまたは他のブラウザ)をロードしますか?

答えて

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!しかし、古いコードを使用するように見えます。例の 'registerHandler'呼び出しをdocs [link](http://vertx.io/docs/vertx-web/java/#_sockjs_event_bus_bridge) で確認し、3.0.xから3.1への変更を[link]で確認してください。 (https://www.npmjs.com/package/vertx3-eventbus-client)。しかし、jsは本当に古いかもしれません、私はCDNから取得しようとします。 – Niels

+0

github [link](https://github.com/vert-x3/vertx-bus-bower)は、npmjsではなく最新のソースのように見えます – Niels