简体   繁体   中英

Play: stop WebSocket actor in case of failure

I'm using Play framework and Akka actors for WebSocket communication. I'm using tryAcceptWithActor method:

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

I would like MyWebSocketActor to Stop when it fails (eg throwing an Exception). Is this the default supervisor strategy? Otherwise how can I define a strategy like this? (I don't know if MyWebSocketActor are created as top-level actors or children of some actor hidden by Play)

Looking at the code of WebSocketActorSupervisor , which is where the Props is converted to actor, has default strategy overridden as

 override def supervisorStrategy = OneForOneStrategy() {
  case _ => SupervisorStrategy.Stop
}

So, Stop is the default behavior.

To assure yourself that Johny's answer is correct override the postStop method of MyWebSocketActor. You will see that it is called when the web-socket is lost.

override def postStop() = {
    super.postStop()
    log.debug("connection lost")
  }

This is also a helpful hook for any additional handlers eg user presence information.

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