【问题标题】:Understanding Concurrent.unicast's arguments理解 Concurrent.unicast 的论点
【发布时间】:2014-04-30 09:00:22
【问题描述】:

我使用 Play Framework 2.2

为了实现 WebSocket 连接,我使用了适合我需要的Concurrent.unicast

val enumerator = Concurrent.unicast[JsValue] {
    channel => userIdWithChannelMap += u.id -> channel
}

但是,Concurrent.unicastsource code 显示了几个参数的需要:

def unicast[E](
    onStart: Channel[E] => Unit,
    onComplete: => Unit = (),
    onError: (String, Input[E]) => Unit = (_: String, _: Input[E]) => ())(implicit ec: ExecutionContext)

我知道当IterateeDone 时会执行onComplete
但是onComplete回调和Iterateemap方法有什么区别:

/**
   *
   * 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{...}

我对@9​​87654336@、onDoneEnumeratingIteratee#map 感到困惑。
谁能解释一下区别?
尤其是,为什么 Concurrent#broadcast 不像 unicast 那样提供 onComplete 参数。

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

【问题讨论】:

    标签: scala websocket playframework-2.2 iterate


    【解决方案1】:

    每一个都是不同的,但差异是微妙的。

    unicastonComplete 参数允许您设置一个回调,用于当它应用到的 Iteratee 成功完成时。单独的 onError 回调用于失败时。

    EnumeratoronDoneEnumerating 方法允许您附加一个回调,如果 Iteratee 成功完成或失败,将调用该回调。

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

    Concurrent.broadcast 不接受 onCompleteonError 回调,因为它可以应用于多个 Iteratee 实例,它们的行为方式不一定相同。一个实例可能会因错误而死亡,而另一个实例稍后会成功完成,而另一个则永远不会完成。因此,运行 Enumerator 的代码对这些事件做出反应不太可能有意义。

    【讨论】:

      猜你喜欢
      • 2014-03-01
      • 1970-01-01
      • 2010-12-13
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      • 2019-01-09
      相关资源
      最近更新 更多