如何使用Play中的Akka流将消息从websocket客户端发送到Akka源

时间:2018-11-19 20:02:33

标签: scala playframework akka akka-stream akka-http

我有一个Akka Source,可以使用Play Akka WebSocket播放数字流。 Websocket客户端可以发送过滤器消息(例如0或1)以获取偶数或奇数消息。

我不明白如何将过滤器发送到源。

感谢任何反馈

def numbersSource(filter: Int) =
  Source(1 to 1000)
  .throttle(1, 1.second, 1, ThrottleMode.Shaping)
  .filter{ e =>
    filter match {
      case 0 =>
        e % 2 == 0
      case 1 =>
        e % 2 != 0
      case _ =>
        true
    }
  }.map(_.toString)

def testFlow(): Flow[String, String, NotUsed] = {
    Flow[String]
       .filter { msg =>
           //message from ws client
           println(">>"+ msg)
          //validate
          msg != null && !msg.isEmpty
       }
       .via(Flow.fromSinkAndSourceCoupled(Sink.ignore, numbersSource(1)))
  }

def testWSFlow ():WebSocket = {

  WebSocket.acceptOrResult[String, String] {

     case rh if sameOriginCheck(rh) =>
         Future.successful(
      Right(testFlow()))

     case rejected =>
      LOGGER.error(s"Request $rejected failed same origin check")
       Future.successful {
         Left(Forbidden("forbidden"))
      }
    }
 }

0 个答案:

没有答案