Created
November 20, 2014 23:11
-
-
Save WadeWaldron/07d6accb484ee3317cd7 to your computer and use it in GitHub Desktop.
Streaming Play Enumerators through Spray (ChunkedResponder)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package chunkedresponses | |
import akka.actor.{Actor, ActorRef} | |
import spray.http.HttpHeaders.RawHeader | |
import spray.http._ | |
object ChunkedResponder { | |
case class Chunk(data: HttpData) | |
case object Shutdown | |
case object Ack | |
} | |
class ChunkedResponder(contentType: ContentType, responder: ActorRef) extends Actor { | |
import ChunkedResponder._ | |
def receive:Receive = { | |
case chunk: Chunk => | |
responder.forward(ChunkedResponseStart(HttpResponse(entity = HttpEntity(contentType, chunk.data))).withAck(Ack)) | |
context.become(chunking) | |
case Shutdown => | |
responder.forward(HttpResponse(headers = List(RawHeader("Content-Type", contentType.value))).withAck(Ack)) | |
context.stop(self) | |
} | |
def chunking:Receive = { | |
case chunk: Chunk => | |
responder.forward(MessageChunk(chunk.data).withAck(Ack)) | |
case Shutdown => | |
responder.forward(ChunkedMessageEnd().withAck(Ack)) | |
context.stop(self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment