Created
January 4, 2013 10:17
-
-
Save jboner/4451490 to your computer and use it in GitHub Desktop.
Nice simple Akka IO HTTP Client by @patriknw
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 akka.actor._ | |
import java.net.InetSocketAddress | |
import akka.util.ByteString | |
class SimpleClient(destAddress: InetSocketAddress) extends Actor { | |
val socket = IOManager(context.system) connect (destAddress) | |
val state = IO.IterateeRef.sync() | |
def receive = { | |
case IO.Connected(`socket`, address) ⇒ | |
println("Connected") | |
socket.asWritable write ByteString("GET / HTTP/1.1\n") | |
socket.asWritable write ByteString("host: " + destAddress.getHostName + "\n\n") | |
case IO.Read(`socket`, bytes) ⇒ | |
println("Read: " + bytes.utf8String) | |
case IO.Closed(`socket`, cause) ⇒ | |
println("Closed") | |
state(cause) | |
} | |
override def postStop { | |
socket.close() | |
state(IO EOF) | |
} | |
} | |
val client = system.actorOf(Props(new SimpleClient(new InetSocketAddress("www.sunet.se", 80)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment