如何使用akka流重构此代码.

Car*_*rez 8 akka playframework playframework-2.5

我们的想法是保持频道开放以便以后使用它.在playframework 2.5.x中,文档说你必须使用akka流,但没有说明如何实现这个例子.有人可以帮帮我吗?

import play.api.mvc._
import play.api.libs.iteratee._
import play.api.libs.concurrent.Execution.Implicits.defaultContext

def socket =  WebSocket.using[String] { request =>

  // Concurrent.broadcast returns (Enumerator, Concurrent.Channel)
  val (out, channel) = Concurrent.broadcast[String]

  // log the message to stdout and send response back to client
  val in = Iteratee.foreach[String] {
    msg => println(msg)
      // the Enumerator returned by Concurrent.broadcast subscribes to the channel and will
      // receive the pushed messages
      channel push("I received your message: " + msg)
  }
  (in,out)
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*nso -1

我认为您只是在寻找如何与 Play 2.5 和 Akka Streams 流程进行 Echo websocket 连接。

这应该可以解决问题

  def socket = WebSocket.accept[String, String] { request =>
    Flow[String]
      .map(msg => "I received your message: " + msg)
  }
Run Code Online (Sandbox Code Playgroud)