我使用Play Framework2.2
为了实现WebSocket连接,我使用了适合我需要的Concurrent.unicast:
val enumerator = Concurrent.unicast[JsValue] {
channel => userIdWithChannelMap += u.id -> channel
}但是,源代码 of Concurrent.unicast显示了几个参数的需要:
def unicast[E](
onStart: Channel[E] => Unit,
onComplete: => Unit = (),
onError: (String, Input[E]) => Unit = (_: String, _: Input[E]) => ())(implicit ec: ExecutionContext)据我所知,onComplete是在Iteratee是Done时执行的。
然而,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的需求是什么,出现在源代码中。
实际上,我看到了WebSocket的一些实现,涉及:
Concurrent.unicast{...}.onDoneEnumerating{...}我和onComplete,onDoneEnumerating和Iteratee#map混在一起。
有人能解释一下差异吗?
特别是,为什么Concurrent#broadcast不像unicast那样提出onComplete参数。
很难找到一些关于Iteratee世界的好文档。
发布于 2014-04-30 01:00:22
每一种情况都是不同的,但差别是微妙的。
onComplete参数的unicast允许您为应用它的Iteratee成功完成时设置一个回调。单独的onError回调用于当它失败时使用。
onDoneEnumerating方法的Enumerator允许您附加一个回调,如果Iteratee成功完成或失败,将调用该回调。
Iteratee.map允许您将回调附加到成功完成的Iteratee,并更改它使用它完成的值。
Concurrent.broadcast不接受onComplete或onError回调,因为它可以应用于多个Iteratee实例,而这些实例的行为方式不一定相同。一个实例可能会因错误而死亡,而另一个实例可能会在稍后成功完成,而另一个实例则永远不会完成。因此,运行枚举数的代码不太可能对这些事件作出反应。
https://stackoverflow.com/questions/23371586
复制相似问题