akka-http send continuous chunked http response (stream)

て烟熏妆下的殇ゞ 提交于 2019-11-29 15:13:46

问题


I have this crude test example with akka-http client and server.

Server.scala:

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Sink
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model._
import scala.concurrent.Future

class Server extends Runnable {

    def run() = {

        implicit val system = ActorSystem("server")
        implicit val materializer = ActorMaterializer()

        val serverSource = Http().bind(interface = "localhost", port = 8200)

        val requestHandler: HttpRequest => HttpResponse = {
            case HttpRequest(GET, Uri.Path("/stream"), _, _, _) =>
                HttpResponse(entity = HttpEntity(MediaTypes.`text/plain`, "test"))
        }

        val bindingFuture: Future[Http.ServerBinding] = serverSource.to(Sink.foreach { connection =>
            connection handleWithSyncHandler requestHandler
        }).run()

    }

}

Client.scala:

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

object Client extends App {

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

    new Thread(new Server).start()

    implicit val materializer = ActorMaterializer()
    val source = Uri("http://localhost:8200/stream")
    val finished = Http().singleRequest(HttpRequest(uri = source)).flatMap { response =>
        response.entity.dataBytes.runForeach { chunk =>
            println(chunk.utf8String)
        }
    }

}

At the moment Server just replies with a single "test".

How do I change the HttpResponse in Server to send "test" as chunked (stream) in an endless loop every 1 second?


回答1:


Found the answer.

Server.scala:

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Source, Sink}
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model._
import scala.concurrent.Future
import scala.concurrent.duration._

class Server extends Runnable {

    def run() = {

        implicit val system = ActorSystem("server")
        implicit val materializer = ActorMaterializer()

        val serverSource = Http().bind(interface = "localhost", port = 8200)

        val requestHandler: HttpRequest => HttpResponse = {
            case HttpRequest(GET, Uri.Path("/stream"), _, _, _) =>
                HttpResponse(entity = HttpEntity.Chunked(ContentTypes.`text/plain`, Source(0 seconds, 1 seconds, "test")))
        }

        val bindingFuture: Future[Http.ServerBinding] = serverSource.to(Sink.foreach { connection =>
            connection handleWithSyncHandler requestHandler
        }).run()

    }

}


来源:https://stackoverflow.com/questions/33123280/akka-http-send-continuous-chunked-http-response-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!