Created
September 6, 2011 23:01
-
-
Save bkyrlach/1199244 to your computer and use it in GitHub Desktop.
Client/Server with remote actors...
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
/* | |
* This is an attempt to keep references to client actors on a remote actor to push events out to them as appropriate. | |
* Note that, as per the comment in the client actor, the console output shows up on the server console not the client(s) | |
* console(s). | |
* | |
*/ | |
package com.kyrlach.scratch | |
import akka.actor.{Actor,ActorRef} | |
import akka.actor.Actor._ | |
case class Send(s:String) | |
case class Rec(s:String) | |
class ServerActor extends Actor { | |
var clients:Set[ActorRef] = Set() | |
def receive = { | |
case "login" => clients = clients + self.sender.get | |
case Send(s:String) => { | |
clients foreach { c => c ! Rec(s) } | |
} | |
} | |
} | |
class ClientActor extends Actor { | |
Actor.remote.start("localhost",2553) //Not sure this needs to be here... | |
def receive = { | |
case "login" => Client.server ! "login" | |
case s:Send => Client.server ! s | |
case Rec(s:String) => println(s) //This output appears in the server console!!!! | |
//If you comment out the Actor.remote.start line above. | |
} | |
} | |
object Server { | |
def run = { | |
Actor.remote.start("localhost",2552) | |
Actor.remote.register("server", actorOf[ServerActor]) | |
} | |
def main(args:Array[String]):Unit = run | |
} | |
object Client { | |
val actor = remote.actorFor("server", "localhost", 2552) | |
def server = actor | |
def main(args:Array[String]):Unit = { | |
val c1 = actorOf[ClientActor].start | |
val c2 = actorOf[ClientActor].start | |
c1 ! "login" | |
c2 ! "login" | |
c1 ! Send("Hi") | |
c2 ! Send("How are you?") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment