Scala读取连续的HTTP流 [英] Scala read continuous http stream

查看:58
本文介绍了Scala读取连续的HTTP流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在scala中连接并读取连续的(分块)http流?例如,如果我用python/bottle编写了这个简单的服务:

How can I connect to and read a continuous (chunked) http stream in scala? For example, if I have this simple service written in python/bottle:

from gevent import monkey; monkey.patch_all()

import gevent
from bottle import route, run

@route('/stream')
def stream():
    while True:
        yield 'blah\n'
        gevent.sleep(1)

run(host='0.0.0.0', port=8100, server='gevent')

我打算使用 akka-stream 处理数据,我只需要一种检索数据的方法.

I'm planning to use akka-stream to process the data, I just need a way to retrieve it.

推荐答案

这应该有效.基本上,您对会产生分块响应的uri发出单个请求.响应实体包含一个dataBytes流.在分块响应的情况下,这将是分块流.如果是非分块响应(HttpEntity.Strict),则该流将只有一个块.

This should work. Basically, you do a single request to an uri that produces a chunked response. The response entity contains a dataBytes stream. In case of a chunked response, this will be the stream of chunks. In case of a non-chunked response (HttpEntity.Strict), this will be a stream with just a single chunk.

显然,您也可以在实体上进行显式匹配,以查看其是否为HttpEntity.Chunked,但通常您也希望保留处理非分块响应的功能.

Obviously you can also explicitly match on the entity to see if it is HttpEntity.Chunked, but usually you want to retain the ability to handle non-chunked responses as well.

在现实世界的应用程序中,您不会使用runForeach来执行副作用,而是对dataBytes流进行一些处理.

In a real world application you would not use runForeach to execute a side effect, but do some processing with the dataBytes stream.

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{Uri, HttpRequest}
import akka.stream.ActorMaterializer

object ChunkTestClient extends App {

  implicit val system = ActorSystem("test")
  import system.dispatcher

  implicit val materializer = ActorMaterializer()
  val source = Uri("https://jigsaw.w3.org/HTTP/ChunkedScript")
  val finished = Http().singleRequest(HttpRequest(uri = source)).flatMap { response =>
    response.entity.dataBytes.runForeach { chunk =>
      println(chunk.utf8String)
    }
  }
}

这篇关于Scala读取连续的HTTP流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆