2013-10-29 111 views
2

如何關閉WebSocket連接?如果您想立即關閉它,documentation上的示例有效。PlayFramework 2.2 scala關閉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) 
    } 
    } 

謝謝你的幫忙!

回答

3

我知道了:我不得不去通過我在

var outEnumerator: Enumerator[String] = Concurrent.unicast(c => channel = Some(c)) 

,打開了通道的註釋塊將成爲

if (gotString == "close_me") { 
    channel.foreach(_.eofAndEnd())  
} 

將通過枚舉和關閉推送EOF連接。

相關問題