简体   繁体   English

Gzip在游戏框架中重复响应

[英]Gzip chunked response in play framework

Does anybody know how to compress chunked response on play framework 2.2.1? 有人知道如何在play framework 2.2.1上压缩chunked响应吗? I have gzipFilter which perfectly compress regular content, however doesnt compress chunked responses. 我有gzipFilter,可以完美地压缩常规内容,但不会压缩分块响应。 My code is pretty straightforward, something like this: 我的代码非常简单,如下所示:

return ok(new StringChunks() {
    public void onReady(Chunks.Out<String> out) {
        out.write("Huuuge content to be gzipped");
        out.write("Huuuge content2 to be gzipped");
    }
});

As far as I understand, you have to Gzip the response before chunking it. 据我所知,你必须在分块之前对响应进行Gzip。 If play chunks it, you can't ask Apache to gzip afterwards, nor a play filter. 如果播放它,你不能要求Apache随后进行gzip,也不能使用播放过滤器。

I got something working with the following code. 我得到了一些使用以下代码的东西。 It's scala but you can get the idea :) (Play 2.2 Scala) 这是scala,但你可以得到这个想法:)(播放2.2 Scala)

  import scala.concurrent.ExecutionContext.Implicits.global
  val enumerator = Enumerator.outputStream { outputStream =>
    val finalOutputStream = {
      if ( canGzip ) new GZIPOutputStream(outputStream)
      else outputStream
    }
    CustomSerializer.serialize(call,finalOutputStream)
  }

  val headers = List(
    Some("Content-Type","application/json"),
    if (canGzip) Some(("Content-Encoding","gzip")) else None
  ).flatten

  Ok.chunked(enumerator).withHeaders(headers: _*)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM