I'm playing around with vertx and vertx-web. I have a simple endpoint that produces a chunked response by endlessly printing "hello" once a second.
How do I detect when the client disconnects? Vertx doesn't seem to throw an exception when attempting to write to a disconnected client, and routingContext.response().closed()
keeps returning false long after the client is gone.
Here's a minimal example, I'm using curl as the client:
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
HttpServer server = vertx.createHttpServer();
Router router = Router.router(vertx);
router.route("/hello").handler(rc -> {
rc.response().setChunked(true).putHeader("Content-Type", "text/plain");
vertx.setPeriodic(1000, l -> {
if (rc.response().closed()) {
System.out.println("Stopping"); //Doesn't happen
vertx.cancelTimer(l);
}
try {
System.out.println("hello"); // Keeps printing long after the client disconnects
rc.response().write("hello\n");
} catch (Exception e) {
e.printStackTrace(); // Doesn't happen
rc.response().close();
}
});
});
server.requestHandler(router::accept).listen(8080);
}