Created
February 22, 2021 15:41
-
-
Save johanandren/47c325c873506a309fcf4a2767a59821 to your computer and use it in GitHub Desktop.
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
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