我使用Play Framework 2.2
为了实现WebSocket连接,我使用了符合我需求的Concurrent.unicast
:
val enumerator = Concurrent.unicast[JsValue] {
channel => userIdWithChannelMap += u.id -> channel
}
但是,Concurrent.unicast
的{{3}}显示了几个参数的需求:
def unicast[E](
onStart: Channel[E] => Unit,
onComplete: => Unit = (),
onError: (String, Input[E]) => Unit = (_: String, _: Input[E]) => ())(implicit ec: ExecutionContext)
我了解当onComplete
为Iteratee
时会执行Done
但onComplete
回调与map
的{{1}}方法之间的区别是什么:
Iteratee
此外,source code中提出的/**
*
* 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)
需要什么
实际上,我遇到了WebSocket的一些实现:
Enumerator#onDoneEnumerating
我对Concurrent.unicast{...}.onDoneEnumerating{...}
,onComplete
和onDoneEnumerating
感到困惑
任何人都可以解释这些差异吗?
特别是,为什么Iteratee#map
不会像Concurrent#broadcast
那样提出onComplete
参数。
很难找到关于unicast
世界的一些好文档。
答案 0 :(得分:2)
每一种都不同,但差异很微妙。
onComplete
的{{1}}参数允许您设置一个回调函数,以便成功应用它的Iteratee。单独的unicast
回调适用于失败时。
onError
的{{1}}方法允许您附加一个回调,如果onDoneEnumerating
成功完成或失败,将会调用该回调。
Enumerator
允许您将回调附加到成功完成的Iteratee
,并更改其完成的值。
Iteratee.map
不会执行Iteratee
或Concurrent.broadcast
回调,因为它可以应用于多个onComplete
个实例,这些实例的行为方式不一定相同。一个实例可能会因错误而死亡,而另一个实例可能会在以后成功完成,而另一个实例永远不会完成。因此,运行Enumerator的代码不太可能对这些事件作出反应。