Last active
June 15, 2019 17:10
-
-
Save jasonmartens/f846730f170020dc16a3e3aa489886b7 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 scalaz.nio._ | |
import scalaz.nio.channels.{AsynchronousServerSocketChannel, AsynchronousSocketChannel} | |
import scalaz.zio.clock.Clock | |
import scalaz.zio.console._ | |
import scalaz.zio.{App, ZIO} | |
object Main extends App { | |
override def run(args: List[String]): ZIO[Environment, Nothing, Int] = | |
myAppLogic | |
.either | |
.map(_.fold(e => { e.printStackTrace(); 1 }, _ => 0)) | |
val myAppLogic: ZIO[Clock with Console, Exception, Unit] = { | |
SocketAddress.inetSocketAddress(1337).map{ addr => | |
AsynchronousServerSocketChannel().map { socket => | |
socket.bind(addr) | |
socket | |
}.bracket(_.close) { socket => | |
socket.accept.bracket(_.close) { conn => | |
doWork(conn).fork | |
}.forever | |
} | |
} | |
} | |
def doWork(work: AsynchronousSocketChannel): ZIO[Console, Exception, Unit] = { | |
for { | |
chunk <- work.read(16) | |
str = chunk.toArray.map(_.toChar).mkString | |
_ <- putStrLn("content: " + str) | |
} yield () | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gives an error when compiling: