2013-10-29 3 views
2

어떻게 웹 소켓 연결을 닫을 수 있습니까? documentation의 예제는 즉시 닫으려는 경우 작동합니다.PlayFramework 2.2 스칼라 닫기 웹 소켓 연결

다음과 같은 경우는 어떻게됩니까? 미래에 어떤 조건이 발생할 때 연결을 닫고 싶습니다. 예를 들어, 클라이언트로부터 특정 메시지를받을 때.

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)) 

와에서 열린 채널을 통해 가야했다 열거 및 닫기를 통해 EOF를 밀어 것입니다

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

연결.