了解Concurrent.unicast的参数

时间:2014-04-29 17:38:30

标签: scala websocket playframework-2.2 iterate

我使用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)

我了解当onCompleteIteratee时会执行DoneonComplete回调与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{...} onCompleteonDoneEnumerating感到困惑 任何人都可以解释这些差异吗? 特别是,为什么Iteratee#map不会像Concurrent#broadcast那样提出onComplete参数。

很难找到关于unicast世界的一些好文档。

1 个答案:

答案 0 :(得分:2)

每一种都不同,但差异很微妙。

onComplete的{​​{1}}参数允许您设置一个回调函数,以便成功应用它的Iteratee。单独的unicast回调适用于失败时。

onError的{​​{1}}方法允许您附加一个回调,如果onDoneEnumerating成功完成或失败,将会调用该回调。

Enumerator允许您将回调附加到成功完成的Iteratee,并更改其完成的值。

Iteratee.map不会执行IterateeConcurrent.broadcast回调,因为它可以应用于多个onComplete个实例,这些实例的行为方式不一定相同。一个实例可能会因错误而死亡,而另一个实例可能会在以后成功完成,而另一个实例永远不会完成。因此,运行Enumerator的代码不太可能对这些事件作出反应。