Question

I'm forwarding some real-time data to web clients using chunked encoding.

In the following way, I could make HTTP streaming responses,

public Result action() {
    return ok(new StringChunks() {
        public void onReady(final Out<String> out) {
            openStream().onData(new Consumer<String>() {
                public void accept(String string) {
                    out.write(string);
                }
            }
        }
    }
}

But I need to clean up some resources after the client has disconnected. (by e.g. closing the browser window or when the stream reaches EOF)

When using WebSocket I could detect client disconnection using Iteratee.mapDone.

Is there an equivalent method to detect it when using Chunks?

Thanks

Was it helpful?

Solution

Well, just figured it out.

Results.Chunked.Out<A> object has onDisconnected(Callback0) method that I can register a disconnection callback. so

public Result action() {
    return ok(new StringChunks() {
        public void onReady(final Out<String> out) {
            out.onDisconnected(new F.Callback0() {
                public void invoke() throws Throwable {
                    // clean up things ...
                }
            }
            openStream().onData(new Consumer<String>() {
                public void accept(String string) {
                    out.write(string);
                }
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top