在Scala中播放2.2.2-WebSocket/相当于in.onClose()的内容

在Scala中播放2.2.2-WebSocket/相当于in.onClose()的内容,scala,websocket,akka,playframework-2.2,Scala,Websocket,Akka,Playframework 2.2,我对Scala使用Play 2.2.2。 我的控制器中有以下代码: def wsTest = WebSocket.using[JsValue] { implicit request => val (out, channel) = Concurrent.broadcast[JsValue] val in = Iteratee.foreach[JsValue] { msg => println(msg

我对Scala使用Play 2.2.2。 我的控制器中有以下代码:

    def wsTest = WebSocket.using[JsValue] {
        implicit request =>
          val (out, channel) = Concurrent.broadcast[JsValue]
          val in = Iteratee.foreach[JsValue] {
            msg => println(msg)
          }
          userAuthenticatorRequest.tracked match { //detecting wheter the user is authenticated
            case Some(u) =>
              mySubscriber.start(u.id, channel)
            case _ =>
              channel push Json.toJson("{error: Sorry, you aren't authenticated yet}")
          }
          (in, out)
      }
调用此代码:

object MySubscriber {

  def start(userId: String, channel: Concurrent.Channel[JsValue]) {
    ctx.getBean(classOf[ActorSystem]).actorOf(Props(classOf[MySubscriber], Seq("comment"), channel), name = "mySubscriber") ! "start"
    //a simple refresh would involve a duplication of this actor!
  }

}

class MySubscriber(redisChannels: Seq[String], channel: Concurrent.Channel[JsValue]) extends RedisSubscriberActor(new InetSocketAddress("localhost", 6379), redisChannels, Nil) with ActorLogging {

  def onMessage(message: Message) {
    println(s"message received: $message")
    channel.push(Json.parse(message.data))
  }

  override def onPMessage(pmessage: PMessage) {
    //not used
    println(s"message received: $pmessage")
  }
}
问题是,当用户刷新页面时,一个新的websocket会重新启动,涉及名为
mySubscriber
的参与者的复制

我注意到该剧的Java版本有一种检测关闭连接的方法,可以关闭一个演员。 例如:


如何使用Scala WebSocket API处理同样的事情?每次关闭套接字时,我都要关闭actor。

正如@Mik378所建议的,
Iteratee.map
充当
onClose
的角色

val in = Iteratee.foreach[JsValue] {
  msg => println(msg)
} map { _ =>
  println("Connection has closed")
}

听起来像是
Iteratee.map
mapDone
在2.2.2中被弃用)在WebSocket中充当inClose,但任何确认都很酷:)谢谢您的确认;)
val in = Iteratee.foreach[JsValue] {
  msg => println(msg)
} map { _ =>
  println("Connection has closed")
}