我使用其原生的Enumerator构造在Play中成功设置了一个websocket,调用一些返回String的代码:
def operationStatusFeed = WebSocket.using[String] { implicit request =>
val in = Iteratee.ignore[String]
val out = Enumerator.repeatM {
Promise.timeout(operation, 3 seconds)
}
(in, out)
}
现在我希望我的operation
函数返回一个rx.lang.scala.Observable[String]
而不是一个String,并且我想在输入时立即输出任何String。如何将此Observable映射到play.api.libs.iteratee.Enumerator
?
答案 0 :(得分:2)
你可以使用Bryan Gilbert的隐式转换。这将完美地工作,但要小心使用updated version of Bryan Gilbert's conversions!在Jeroen Kransen的答案中从未调用取消订阅(这很糟糕!)。
/*
* Observable to Enumerator
*/
implicit def observable2Enumerator[T](obs: Observable[T]): Enumerator[T] = {
// unicast create a channel where you can push data and returns an Enumerator
Concurrent.unicast { channel =>
val subscription = obs.subscribe(new ChannelObserver(channel))
val onComplete = { () => subscription.unsubscribe }
val onError = { (_: String, _: Input[T]) => subscription.unsubscribe }
(onComplete, onError)
}
}
class ChannelObserver[T](channel: Channel[T]) extends rx.lang.scala.Observer[T] {
override def onNext(elem: T): Unit = channel.push(elem)
override def onCompleted(): Unit = channel.end()
override def onError(e: Throwable): Unit = channel.end(e)
}
要完成,这是从Enumerator到Observable的转换:
/*
* Enumerator to Observable
*/
implicit def enumerator2Observable[T](enum: Enumerator[T]): Observable[T] = {
// creating the Observable that we return
Observable({ observer: Observer[T] =>
// keeping a way to unsubscribe from the observable
var cancelled = false
// enumerator input is tested with this predicate
// once cancelled is set to true, the enumerator will stop producing data
val cancellableEnum = enum through Enumeratee.breakE[T](_ => cancelled)
// applying iteratee on producer, passing data to the observable
cancellableEnum (
Iteratee.foreach(observer.onNext(_))
).onComplete { // passing completion or error to the observable
case Success(_) => observer.onCompleted()
case Failure(e) => observer.onError(e)
}
// unsubscription will change the var to stop the enumerator above via the breakE function
new Subscription { override def unsubscribe() = { cancelled = true } }
})
}
Rx for Play中的WebSockets
另一方面,您可能会注意到,在Play中处理Iteratees和Enumerators的大部分时间都是在使用WebSockets时(就像您在这里一样)。我们都同意Iteratees实际上不那么直观,Observables这可能就是你在Play项目中使用Rx的原因。
从这个观察结果来看,我已经构建了一个名为 WidgetManager 的库,它正是这样做的:在Play中集成Rx以消除Iteratees操作。
使用该库,您的代码可能只是:
def operationStatusFeed = WebSocket.using[String] { implicit request =>
// you can optionally give a function to process data from the client (processClientData)
// and a function to execute when connection is closed (onClientClose)
val w = new WidgetManager()
w.addObservable("op", operation)
// subscribe to it and push data in the socket to the client (automatic JS callback called)
w.subscribePush("op")
// deals with Iteratees and Enumerators for you and returns what's needed
w.webSocket
}
图书馆在GitHub上: RxPlay (欢迎提供参考资料)
答案 1 :(得分:0)
我的灵感来自Brian Gilbert:
class ChannelObserver[T](chan: Channel[T]) extends Observer[T] {
override def onNext(arg: T): Unit = chan.push(arg)
override def onCompleted(): Unit = chan.end()
override def onError(e: Throwable): Unit = chan.end(e)
override val asJavaObserver: rx.Observer[T] = new rx.Observer[T] {
def onCompleted() {
chan.end()
}
def onError(e: Throwable) {
chan.end(e)
}
def onNext(arg: T) {
chan.push(arg)
}
}
}
implicit def observable2Enumerator[T](obs: Observable[T]): Enumerator[T] = {
Concurrent.unicast[T](onStart = { chan =>
obs.subscribe(new ChannelObserver(chan))
})
}
隐式函数将Observables转换为枚举数,而无需任何其他代码。