Skip to content

Instantly share code, notes, and snippets.

@jboner
Created January 4, 2013 10:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jboner/4451490 to your computer and use it in GitHub Desktop.
Save jboner/4451490 to your computer and use it in GitHub Desktop.
Nice simple Akka IO HTTP Client by @patriknw
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