我在Scala中使用Play Framework和WebSocket,我尝试接受Actor。
如果Web服务器关闭了连接,客户端可以通过JS中的onClose
触摸它,如下所示:
websocket.onclose = function (evt) { onClose(evt) };
但是服务器端呢?
如果客户端关闭浏览器,Web服务器是否可以触摸此关闭事件?
答案 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")
}
}