将值传递给创建处理websocket Play 2的Actor

时间:2016-03-03 19:24:16

标签: scala playframework playframework-2.0

我接受以下方式的websocket消息:

  def socket = WebSocket.tryAcceptWithActor[ChatCommand, JsValue] { request =>
    Future.successful(request.session.get("login") match {
      case None => Left(Forbidden)
      case Some(_) => Right(ChatActor.props)
    })
  }

如何将“login”的值传递给actor?

编辑:

object ChatActor{
  def props(out: ActorRef) = Props(new ChatActor(out))
 ....
}

class ChatActor(out: ActorRef) extends Actor {

  def receive = ...

}

当我改变道具时,我没有在控制器中供应。

1 个答案:

答案 0 :(得分:1)

如果聊天演员类似

object ChatActor {
  def props(out: ActorRef, login: String): Props = Props(new ChatActor(out, login))
}

class ChatActor(out: ActorRef, login: String) extends Actor{
  override def receive: Receive = ???
}

然后你应该编码

  def socket = WebSocket.tryAcceptWithActor[ChatCommand, JsValue] { request =>
    Future.successful(request.session.get("login") match {
      case None => Left(Forbidden)
      case Some(login) => Right(out => ChatActor.props(out, login))
    })
  }

您可能会发现有趣的http://doc.akka.io/docs/akka/snapshot/scala/actors.html#Props