Created
October 17, 2012 03:51
-
-
Save bobofzhang/3903608 to your computer and use it in GitHub Desktop.
tcp server in akka&scalla, create actor for each connected client.
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
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