7

文档指出,spray 能够处理分块响应,但我找不到任何示例。有我天真的实现:

object Main extends App {

  implicit val system = ActorSystem()
  import system.dispatcher
  val log = Logging(system, getClass)
  val ioBridge = IOExtension(system).ioBridge()
  val httpClient = system.actorOf(Props(new HttpClient(ioBridge)))

  val conduit = system.actorOf(
    props = Props(new HttpConduit(httpClient, "localhost", 3000)),
    name = "http-conduit"
  )

  val pipeline = HttpConduit.sendReceive(conduit)
  val response = pipeline(
    HttpRequest(
      method = GET,
      uri = "/output.cgi.xml"
    )
  )

  response onComplete {
    case Success(a) =>
      log.info("Success: " + a)
      system.shutdown()

    case Failure(error) =>
      log.error(error, "Failure")
      system.shutdown()
  }

}

我已经设置response-chunk-aggregation-limit = 0了,仍然没有任何反应。

你能给我提供阅读分块响应的例子吗?

更新

我重写了我的代码如下:

object Main extends App {

  implicit val system = ActorSystem()
  import system.dispatcher
  val log = Logging(system, getClass)
  val ioBridge = IOExtension(system).ioBridge()
  val httpClient = system.actorOf(Props(new HttpClient(ioBridge)))

  actor(new Act {
    httpClient ! Connect(new InetSocketAddress("localhost", 3000))

    become {
      case Connected(_) =>
        log.info("connected")
        sender ! HttpRequest(GET, "/output.cgi.xml")
      case Closed(handle, reason) =>
        log.info("closed: " + reason)
        system.shutdown()
      case ChunkedResponseStart(res) =>
        log.info("start: " + res)
      case MessageChunk(body, ext) =>
        log.info("chunk: " + body)
      case ChunkedMessageEnd(ext, trailer) =>
        log.info("end: " + ext)
      case m =>
        log.info("received unknown message " + m)
        system.shutdown()
    }
  })

}

现在我在closed: ProtocolError(Aggregated response entity greater than configured limit of 1048576 bytes)建立连接后收到。

我的应用程序.conf

spray.can {
  client {
    response-chunk-aggregation-limit = 0
  }
}
4

1 回答 1

9

正如您所注意到的,HttpConduit 仅适用于聚合响应。您必须跌落到喷雾罐层才能处理单个块。

不幸的是,我们目前没有示例显示您将如何执行此操作。大致上,它是这样工作的(在 M7 中):

  1. response-chunk-aggregation-limit = 0
  2. 发送Connect到 httpClient 演员并等待Connected
  3. 发送给消息HttpRequest的发件人Connected
  4. 处理分块的请求消息ChunkedResponseStartMessageChunkChunkedResponseEnd

有关更多信息,请参阅http://spray.io/documentation/spray-can/http-client/#chunked-responses

与 using 相比HttpConduit,这意味着您必须自己管理连接(如果这就是您使用 HttpConduit 的原因)。在最近的 nightlies 中,它变得更容易了,因为新的HttpClient自动支持连接池等。如果您需要,您还可以在邮件列表中获取更多信息。

于 2013-05-02T14:11:40.163 回答