我对 akka typed 有点陌生,我试图发送一条需要在给定时间内回答的消息。
我发现了带有 ask 的请求-响应模式,这看起来很有趣,但是有没有办法在已经定义的 Behaviours.receive 中实现它?
这里的想法是每次玩家回答或超时后调用 nextPlayerTurn
override def refereeTurn(): Behavior[Msg] = Behaviors.receive {
case (_, msg: GuessMsg) =>
if(currentPlayer.isDefined && currentPlayer.get == msg.getSender) {
controller ! msg
} else {
println("Player tried to guess after Timeout")
}
Behaviors.same
case (context, msg: ReceivedResponseMsg) =>
if(currentPlayer.isDefined && currentPlayer.get == msg.getSender)
nextPlayerTurn(context)
Behaviors.same
...
}
...
/**
* Tells to a player to start his turn and sets a timer that defines time in which a player has to make a guess.
* If such guess isn't made, sends that user an end turn message, fails the promise of his turn and allows next
* player to play his turn
*/
override def nextPlayerTurn(ctx: ActorContext[Msg]): Unit = {
implicit val timeout: Timeout = Timeout.timeout
currentPlayer = Option(turnManager.nextPlayer)
ctx.ask[Msg,Msg](currentPlayer.get, ref => YourTurnMsg(ref)) {
case Success(msg: GuessMsg) => println("\n SUCCESS"); msg
case Failure(_) => println(currentPlayer.get +" didn't guess in time"); TurnEnd(currentPlayer.get)
case _ => TurnEnd(currentPlayer.get)
}
}
在这种情况下,在发送 YourTurnMsg 之后,玩家应该使用停止计时器的 GuessMsg 进行响应,这永远不会发生,因为在执行裁判内部匹配的情况下,而不是成功(它总是在超时后给出失败) .
我是否对询问模式有错误的想法,应该只使用计时器做出新的行为?