【发布时间】:2014-04-30 09:00:22
【问题描述】:
我使用 Play Framework 2.2
为了实现 WebSocket 连接,我使用了适合我需要的Concurrent.unicast:
val enumerator = Concurrent.unicast[JsValue] {
channel => userIdWithChannelMap += u.id -> channel
}
但是,Concurrent.unicast 的 source code 显示了几个参数的需要:
def unicast[E](
onStart: Channel[E] => Unit,
onComplete: => Unit = (),
onError: (String, Input[E]) => Unit = (_: String, _: Input[E]) => ())(implicit ec: ExecutionContext)
我知道当Iteratee 是Done 时会执行onComplete。
但是onComplete回调和Iteratee的map方法有什么区别:
/**
*
* Uses the provided function to transform the Iteratee's computed result when the Iteratee is done.
*
* @param f a function for transforming the computed result
* $paramEcSingle
*/
def map[B](f: A => B)(implicit ec: ExecutionContext): Iteratee[E, B] = this.flatMap(a => Done(f(a), Input.Empty))(ec)
此外,Enumerator#onDoneEnumerating 的需求是什么,在 source code 中提出。
确实,我遇到了一些处理 WebSocket 的实现:
Concurrent.unicast{...}.onDoneEnumerating{...}
我对@987654336@、onDoneEnumerating 和Iteratee#map 感到困惑。
谁能解释一下区别?
尤其是,为什么 Concurrent#broadcast 不像 unicast 那样提供 onComplete 参数。
很难找到一些关于 Iteratee 世界的好文档。
【问题讨论】:
标签: scala websocket playframework-2.2 iterate