akka-http send continuous chunked http response (stream)

后端 未结 1 983
无人共我
无人共我 2021-01-01 04:55

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

Server.scala:

import akka.actor.ActorSystem
import akka.stream.ActorMat         


        
相关标签:
1条回答
  • 2021-01-01 05:26

    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()
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题