我不确定我是否遇到了默认超时(我可以在某处设置它吗?),但也许我错过了一些东西。当客户端的浏览器建立 websocket 连接时,我会保持持久性。然后,在断开连接时,我删除了该持久对象。很简单。断开连接通常会在客户端关闭浏览器时触发,但不会在客户端关闭其 wi-fi 连接时触发(在 MacBook Pro 上进行测试,这并不重要)。
在 Scala 控制器中,我记录了每条in
消息,但是当 wi-fi 关闭时,什么也没有发生(从文档中,我希望 EOF?)。
我认为这一定是一个错误,从我如何解释WS 协议 服务器必须关闭 WebSocket 连接,并应该记录问题。但后者不会发生。
val in = Iteratee.foreach[String](x => {
logger.info("Websocket msg: " + x)
// expect EOF?
x match {
case "persist" => // persist some object
}
}).mapDone { x =>
// delete my persisted object (never happens unless browser/tab closed)
}
有人经历过吗?我已经尝试了一个简单的控制器和与我的配置相匹配的东西。ws3
下面的控制器或控制器都ws2
不能解决问题。玩!下面的代码:
object Application extends Controller {
private def ws(out: PushEnumerator[String]) = {
Logger.logger.info("ws()")
val in = Iteratee.foreach[String](x => {
Logger.logger.info("Websocket msg: " + x)
try {
x match {
case "persist" => Logger.logger.info("PERSIST")
}
} catch {
case e: Exception => {
Logger.logger.info("NOT RECOGNIZED COMMAND, NO PERSIST")
}
}
}).mapDone { x =>
Logger.logger.info("STOP PERSIST")
}
in
}
def ws2() = WebSocket.using[String] { request =>
Logger.logger.info("ws2()")
val out = Enumerator.imperative[String]()
val in = ws(out)
(in, out)
}
def ws3() = WebSocket.using[String] { request =>
Logger.logger.info("ws3()")
val out = Enumerator.imperative[String]()
val in = Iteratee.foreach[String](x => {
Logger.logger.info("Websocket msg: " + x)
try {
x match {
case "persist" => Logger.logger.info("PERSIST")
}
} catch {
case e: Exception => {
Logger.logger.info("NOT RECOGNIZED COMMAND, NO PERSIST")
}
}
}).mapDone { x =>
Logger.logger.info("STOP PERSIST")
}
(in, out)
}
def view() = Action { implicit request =>
Ok(views.html.index.render(""))
}
}
视图很简单:
@(message: String) @main("Welcome to Play 2.0") {
@play20.welcome(message) }
<script type="text/javascript" charset="utf-8">
var sock = new WebSocket("ws://192.168.1.120:9001/ws3");
sock.onopen = function(event) {
sock.send('persist');
}
</script>
路线:
GET /ws2 controllers.Application.ws2
GET /ws3 controllers.Application.ws3
GET /view controllers.Application.view