如何关闭 WebSocket 连接?如果您想立即关闭它,文档上的示例有效。
但是以下情况如何:我想在将来发生某些情况时关闭连接。例如,当我收到来自客户端的某个消息时。
def indexWS = WebSocket.using[String] {
request => {
var channel: Option[Concurrent.Channel[String]] = None
var outEnumerator: Enumerator[String] = Concurrent.unicast(c => channel = Some(c))
val myIteratee: Iteratee[String, Unit] = Iteratee.foreach[String] {gotString => {
// received a string from the client
if (gotString == "close_me") {
// outEnumerator = Enumerator.eof // doesn't work
// outEnumerator >>> Enumerator.eof // doesn't work
}
}}
(myIteratee, outEnumerator)
}
}
谢谢您的帮助!