[英]Play Framework 2: Handle client disconnection in chunked response
I'm forwarding some real-time data to web clients using chunked encoding. 我正在使用分块编码将一些实时数据转发到Web客户端。
In the following way, I could make HTTP streaming responses, 通过以下方式,我可以进行HTTP流响应,
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 eg closing the browser window or when the stream reaches EOF)
(例如,通过关闭浏览器窗口或当流到达EOF时)
When using WebSocket I could detect client disconnection using Iteratee .mapDone. 使用WebSocket时,我可以使用Iteratee .mapDone检测到客户端断开连接。
Is there an equivalent method to detect it when using Chunks ? 使用块时是否有等效的方法来检测它?
Thanks 谢谢
Well, just figured it out. 好吧,只是想通了。
Results.Chunked.Out<A>
object has onDisconnected(Callback0)
method that I can register a disconnection callback. Results.Chunked.Out<A>
对象具有onDisconnected(Callback0)
方法,可以注册断开连接回调。 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);
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.