Vertx 3 - SockJS 套接字打开取消
Vertx 3 - SockJS socket opening canceled
我创建了一个新的 Verticle,它应该响应 HTTP 请求和 SockJS 桥接事件。基于这个问题 and vert.x manual https://vertx.io/docs/vertx-web/java/#_sockjs 我创建了这段代码:
Java:
@Override
public void start(Future<Void> startFuture) throws Exception {
startHttpServer(startFuture);
startSockJSHandler();
}
private void startHttpServer(Future<Void> startFuture) {
HttpServer server = vertx.createHttpServer(new HttpServerOptions());
server.requestHandler(req -> {
System.out.println("[" + new Date().toString() + "] Request #" + ++requestCount);
if (req.path().contains("http")) {
req.response().putHeader("Access-Control-Allow-Origin", "*").end("req_num: " + requestCount);
}
}).listen(8080, ar -> startFuture.handle(ar.mapEmpty()));
}
private void startSockJSHandler() {
Router router = Router.router(vertx);
SockJSHandlerOptions sockJSOptions = new SockJSHandlerOptions().setHeartbeatInterval(2000);
SockJSHandler sockJSHandler = SockJSHandler.create(vertx, sockJSOptions);
BridgeOptions bridgeOptions = new BridgeOptions();
bridgeOptions.addInboundPermitted(new PermittedOptions().setAddressRegex(".*")).addOutboundPermitted(new PermittedOptions().setAddressRegex(".*"));
sockJSHandler.bridge(bridgeOptions, be -> {
System.out.println("BRIDGE EVENT: " + be.type().toString());
});
router.route("/eventbus/*").handler(sockJSHandler);
}
Java编写事件总线客户端脚本:
var sock = new SockJS('http://localhost:8080/eventbus/');
sock.onopen = function() {
console.log('open');
sock.send('test');
};
sock.onmessage = function(e) {
console.log('message', e.data);
sock.close();
};
sock.onclose = function() {
console.log('close');
};
HTTP request/response 工作正常,但 SockJS 事件不行。在网络浏览器 'Network' 模块中,我只看到一个 SockJS 请求 (http://localhost:8080/eventbus/info)。 'pending' 状态 8 秒,此后状态更改为 'closed'(最后调用方法 onclose())。
我是不是做错了什么?
HttpServer
必须将请求委托给 Router
。否则什么也不会发生。通常,它被配置为将所有请求委托给 Router
.
server.requestHandler(router::accept).listen(8080);
请参阅文档中的 Basic Vert.x-Web concepts。
我创建了一个新的 Verticle,它应该响应 HTTP 请求和 SockJS 桥接事件。基于这个问题 and vert.x manual https://vertx.io/docs/vertx-web/java/#_sockjs 我创建了这段代码:
Java:
@Override
public void start(Future<Void> startFuture) throws Exception {
startHttpServer(startFuture);
startSockJSHandler();
}
private void startHttpServer(Future<Void> startFuture) {
HttpServer server = vertx.createHttpServer(new HttpServerOptions());
server.requestHandler(req -> {
System.out.println("[" + new Date().toString() + "] Request #" + ++requestCount);
if (req.path().contains("http")) {
req.response().putHeader("Access-Control-Allow-Origin", "*").end("req_num: " + requestCount);
}
}).listen(8080, ar -> startFuture.handle(ar.mapEmpty()));
}
private void startSockJSHandler() {
Router router = Router.router(vertx);
SockJSHandlerOptions sockJSOptions = new SockJSHandlerOptions().setHeartbeatInterval(2000);
SockJSHandler sockJSHandler = SockJSHandler.create(vertx, sockJSOptions);
BridgeOptions bridgeOptions = new BridgeOptions();
bridgeOptions.addInboundPermitted(new PermittedOptions().setAddressRegex(".*")).addOutboundPermitted(new PermittedOptions().setAddressRegex(".*"));
sockJSHandler.bridge(bridgeOptions, be -> {
System.out.println("BRIDGE EVENT: " + be.type().toString());
});
router.route("/eventbus/*").handler(sockJSHandler);
}
Java编写事件总线客户端脚本:
var sock = new SockJS('http://localhost:8080/eventbus/');
sock.onopen = function() {
console.log('open');
sock.send('test');
};
sock.onmessage = function(e) {
console.log('message', e.data);
sock.close();
};
sock.onclose = function() {
console.log('close');
};
HTTP request/response 工作正常,但 SockJS 事件不行。在网络浏览器 'Network' 模块中,我只看到一个 SockJS 请求 (http://localhost:8080/eventbus/info)。 'pending' 状态 8 秒,此后状态更改为 'closed'(最后调用方法 onclose())。
我是不是做错了什么?
HttpServer
必须将请求委托给 Router
。否则什么也不会发生。通常,它被配置为将所有请求委托给 Router
.
server.requestHandler(router::accept).listen(8080);
请参阅文档中的 Basic Vert.x-Web concepts。