Skip to content

Instantly share code, notes, and snippets.

@bobofzhang
Created October 17, 2012 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobofzhang/3903608 to your computer and use it in GitHub Desktop.
Save bobofzhang/3903608 to your computer and use it in GitHub Desktop.
tcp server in akka&scalla, create actor for each connected client.
package test
import akka.actor._
class Slave extends Actor{
def receive = {
case IO.Read(socket, bytes) =>
println("Slave Received incoming data from socket")
case IO.Closed(socket: IO.SocketHandle, cause) =>
println("Slave Socket has closed, cause: " + cause)
}
}
class Master extends Actor{
def receive = {
case IO.Listening(server, address) =>
println("Master is listening on socket " + address)
case IO.NewClient(server) =>
println("Master new incomming connection on server")
val myActor = context.actorOf(Props[Slave])
val socket = server.accept()(myActor)
case IO.Closed(server: IO.ServerHandle, cause) =>
println("Master Server socket has closed, cause: " + cause)
}
}
object TcpServerTest extends App {
val actorSystem = ActorSystem()
val master = actorSystem.actorOf(Props[Master])
val serverSocket = IOManager(actorSystem).listen("localhost", 8812)(master)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment