Play Framework如何处理来自WebSocket的CloseEvent?

时间:2015-11-26 10:18:51

标签: javascript scala playframework

我在Scala中使用Play Framework和WebSocket,我尝试接受Actor。

如果Web服务器关闭了连接,客户端可以通过JS中的onClose触摸它,如下所示:

websocket.onclose = function (evt) { onClose(evt) };

但是服务器端呢?

如果客户端关闭浏览器,Web服务器是否可以触摸此关闭事件?

1 个答案:

答案 0 :(得分:1)

def ws(userId: String) =
WebSocket.tryAcceptWithActor[String, String](request⇒
  Future {
    Right(out ⇒ Props(new WsActor(userId, out)))
  })
class WsActor(val userId: String, val clientActor: ActorRef) extends Actor{
  override def receive: Receive = ???

  @throws[Exception](classOf[Exception])
  override def preStart(): Unit = {

  }
    //when client close 
    //will call it 
  @throws[Exception](classOf[Exception])
  override def postStop(): Unit = {
    println("stop")
  }

}