简体   繁体   中英

Pass value to creating an Actor handling websocket Play 2

I accept websocket messages in following way:

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

How can i pass the value of "login" to actor?

Edit:

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

class ChatActor(out: ActorRef) extends Actor {

  def receive = ...

}

And when i change props i have no out to supply in controller.

If Chat actor is something like

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 = ???
}

Then your should code

  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))
    })
  }

You might find interesting http://doc.akka.io/docs/akka/snapshot/scala/actors.html#Props

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM