Skip to content

Instantly share code, notes, and snippets.

@johanandren
Created February 22, 2021 15:53
Show Gist options
  • Save johanandren/6863be0f27782810c0b2044a3ae8f98c to your computer and use it in GitHub Desktop.
Save johanandren/6863be0f27782810c0b2044a3ae8f98c 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 TcpServerOneResponseThenComplete2 {
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, halfClose = true).runForeach { incomingConnection =>
incomingConnection.handleWith(Flow[ByteString]
.via(framing)
.take(1)
.map { firstFrame =>
println(s"Request: ${firstFrame.utf8String}")
ByteString("Response\n")
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment