當我試圖流期貨爲的Html(在斯卡拉劇2.5.2)的想法是,它們會呈現在屏幕上時,期貨完整。所以我會流一來源作爲字符串這樣的:流的HTML瀏覽器未來完成
def oneFuture = Action { request =>
val source1: Source[String, NotUsed] = fromFuture(sc.makeServiceCall("async1"))
Ok.chunked(source1)
}
其中sc.makeServiceCall
電話:
class ServiceClient @Inject() (ws: WSClient) {
def makeServiceCall(serviceName: String): Future[String] = {
ws.url(s"http://localhost:9000/mock/$serviceName").get().map(_.body)
}
}
這是參照:
class Mock @Inject() (actorSystem: ActorSystem)(implicit exec: ExecutionContext) extends Controller {
def mock(serviceName: String) = Action.async { request =>
serviceName match {
case "async1" => respond("asy1", 1.second)
case "async2" => respond("asy2", 3.second)
}
}
private def respond(data: String, delay: FiniteDuration): Future[Result] = {
val promise: Promise[Result] = Promise[Result]()
actorSystem.scheduler.scheduleOnce(delay) { promise.success(Ok(data)) }
promise.future
}
}
其中1後返回asy1
第二。因此,考慮到這一點,如果我想流式傳輸模板頁,例如,我將如何在瀏覽器中呈現Html。 views.html.async1.async1Message("a simple string")
- 那就是:
@(async1Message: String)
<span style="color: red; font-size: 22px; font-weight: bold;">@async1Message</span>
你試過了什麼?您是否將服務調用的結果映射到模板中,然後將其返回?這似乎更像是一個前端問題,你如何處理javascript中的響應,對吧?端點如何被調用? – thwiegan