Skip to content

Instantly share code, notes, and snippets.

@johanandren
Created February 22, 2021 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johanandren/47c325c873506a309fcf4a2767a59821 to your computer and use it in GitHub Desktop.
Save johanandren/47c325c873506a309fcf4a2767a59821 to your computer and use it in GitHub Desktop.
import akka.actor.ActorSystem
import akka.stream.scaladsl.Flow
import akka.stream.scaladsl.Tcp
import akka.util.ByteString
object TcpServerOneResponseThenComplete {
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem()
import akka.stream.scaladsl.Framing
val framing = Framing.delimiter(ByteString("\n"), 1024)
Tcp().bind("127.0.0.1", 9999).runForeach { incomingConnection =>
incomingConnection.handleWith(Flow[ByteString]
.via(framing)
.prefixAndTail(1)
.map { case (Seq(firstFrame), rest) =>
println(s"Request: ${firstFrame.utf8String}")
rest.run()
ByteString("Response\n")
}.take(1))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment