Last active
August 29, 2015 14:11
-
-
Save julien-truffaut/e0196958e188ca040e4c to your computer and use it in GitHub Desktop.
How to run a Tcp server in the background
This file contains hidden or 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 java.net.InetSocketAddress | |
import scalaz.concurrent.Task | |
import scalaz.stream.tcp | |
import java.util.concurrent.Executors | |
object TcpServerApp extends App { | |
val executor = Executors.newSingleThreadExecutor | |
implicit val S = scalaz.concurrent.Strategy.DefaultStrategy | |
implicit val AG = tcp.DefaultAsynchronousChannelGroup | |
val address = new InetSocketAddress("localhost", 9999) | |
val pipeline = tcp.reads(1024).map(bv => println(s"Received a ByteVector of size: ${bv.size}")) | |
val server = tcp.server(address, concurrentRequests = 2)(pipeline).runLast.run.getOrElse(sys.error("Plop")).run | |
Task.fork(server)(executor).runAsync(println) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment