public
Created

Simple Akka chat between actors

  • Download Gist
AkkaChat.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
package com.czechscala.blank
 
import akka.actor._
 
class AkkaChat extends Actor {
def receive = {
case (name:String, msg:String) => {
println("Server: " +name+": "+msg)
context.actorSelection("../client*") ! (name,msg)
}
}
}
 
class AkkaClient extends Actor {
def receive = {
case (from:String,msg:String) => {println(self.path.name + ": "+from+": "+msg)}
case msg:String => {
context.actorSelection("../server") ! (self.path.name, msg)
}
}
 
}
 
object AkkaChat extends App {
val system = ActorSystem("AkkaChat")
val actor = system.actorOf(Props[AkkaChat], "server")
 
 
val cJano = system.actorOf(Props[AkkaClient], "clientJano")
val cFero = system.actorOf(Props[AkkaClient], "clientFero")
 
cFero ! "ahoj svete"
cJano ! "nazdar"
} }

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.