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
有什麼不對?
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
github [link](https://github.com/vert-x3/vertx-bus-bower)看起來像最新的源代碼,而不是npmjs – Niels