我正在学习Akka,我正在尝试开发一个非常简单的PingPong应用程序。 我想知道为什么我没有期望的输出,这就是:在Akka了解自我的信息
=> Pong
=> Ping
=> Pong
=> Ping
.....
相反,我得到的只是one => Pong.
游戏应用:
import akka.actor.{ActorSystem, Props}
import akka.pattern.ask
import akka.util.Timeout
import scala.concurrent.duration._
import scala.concurrent.Await
case object Ping
case object Pong
object GamePPApp extends App{
implicit val timeout = Timeout(2 second)
val actorSystem = ActorSystem("Ping-Pong")
val gamer1 = actorSystem.actorOf(Props[GamerActor], "gamer1")
// gamer1 ! Ping // I tried this too
// asking for result from actor
val future = (gamer1 ? Ping)
Await.result(future, 2 seconds)
}
GamerActor:
import akka.actor.Actor
class GamerActor extends Actor{
override def receive: Receive = {
case Ping =>
println(s"=> Pong")
sender ! Pong
case Pong =>
println(s"=> Ping")
sender ! Ping
}
}
任何人都可以向我解释为什么?
[编辑]如果我改变发送者的自我,它的作品,但我想知道为什么我不能用发送者,并在我的情况,我认为自己是发送者
谢谢
嗯......您要发送的消息发回'sender'而不是'self' –
是的,但自我是发件人在我的情况? – elarib
不,它仍然是为'ask'模式创建的临时演员。只要在'sender'上打印一下,就会是'Ping-Pong $ a',而不是你的'gamer1'演员 – kosii