当我试图流期货为的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