本文整理汇总了Scala中play.api.libs.iteratee.Iteratee类的典型用法代码示例。如果您正苦于以下问题:Scala Iteratee类的具体用法?Scala Iteratee怎么用?Scala Iteratee使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Iteratee类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ProxyResponse
//设置package包名称以及导入依赖的类
package services
import java.util.concurrent.TimeUnit
import play.api.http.HeaderNames
import play.api.libs.iteratee.Iteratee
import play.api.mvc.{Result, Results}
import scala.concurrent.Await
import scala.concurrent.duration.Duration
object ProxyResponse {
def apply(result: Result): ProxyResponse = {
val body = new String(contentAsBytes(result))
val httpMessage = HttpMessage(result.header.headers.mapValues(_.split(",").toList), Some( body))
new ProxyResponse(result.header.status, httpMessage)
}
private def contentAsBytes(result: Result): Array[Byte] = {
val eBytes = result.header.headers.get(HeaderNames.TRANSFER_ENCODING) match {
case Some("chunked") => result.body &> Results.dechunk
case _ => result.body
}
Await.result(eBytes |>>> Iteratee.consume[Array[Byte]](), Duration(1, TimeUnit.SECONDS))
}
}
case class ProxyResponse(status: Int = 200, httpMessage: HttpMessage = HttpMessage(Map.empty, None)) {
def simpleHeaderMap: Map[String, String] = {
httpMessage.headers.map {
case (k, v) => (k, v.mkString(","))
}
}
}
示例2: IterateeSpec
//设置package包名称以及导入依赖的类
import scala.concurrent.Future
import play.api.libs.iteratee.Iteratee
import anorm._
import acolyte.jdbc.AcolyteDSL.withQueryResult
import acolyte.jdbc.RowLists.stringList
import acolyte.jdbc.Implicits._
object IterateeSpec extends org.specs2.mutable.Specification {
"Play Iteratee" title
"Iteratees" should {
"broadcast the streaming result" in (
withQueryResult(stringList :+ "A" :+ "B" :+ "C")) { implicit con =>
Iteratees.from(SQL"SELECT * FROM Test", SqlParser.scalar[String]).
run(Iteratee.getChunks[String]) must beEqualTo(List("A", "B", "C")).
await(1000)
}
}
}
示例3: MyWebSocket
//设置package包名称以及导入依赖的类
package controllers
import de.htwg.se.setGame.controller.{NewGame, StartGame, UpdateGame}
import play.api.Logger
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.iteratee.{Concurrent, Iteratee}
import play.api.mvc.WebSocket
import scala.swing.Reactor
class MyWebSocket(private val manager: GameManager) extends Reactor {
def get: WebSocket = WebSocket.using[String] { request =>
val session = new SessionHandler(request.session)
val controller = manager.get(session.getSessionId)
val in = Iteratee.foreach[String](_ => {}).map { _ =>
Logger.debug(MyWebSocket.Disconnect.format(session.getSessionId))
}
val (out, channel) = Concurrent.broadcast[String]
Logger.debug(MyWebSocket.Connected.format(session.getSessionId))
listenTo(controller.getController)
reactions += {
case e: NewGame =>
Logger.debug(MyWebSocket.EventNewGame.format(session.getSessionId))
channel.push(e.getClass.getName)
case e: StartGame =>
Logger.debug(MyWebSocket.EventStartGame.format(session.getSessionId))
channel.push(e.getClass.getName)
case e: UpdateGame =>
Logger.debug(MyWebSocket.EventUpdateGame.format(session.getSessionId))
channel.push(e.getClass.getName)
}
(in, out)
}
}
object MyWebSocket {
val Disconnect = "%s - disconnect from websocket"
val Connected = "%s - connected to websocket"
val EventNewGame = "%s - websocket received 'NewGame' event"
val EventStartGame = "%s - websocket received 'StartGame' event"
val EventUpdateGame = "%s - websocket received 'UpdateGame' event"
}
示例4: ResponseTimeLogFilter
//设置package包名称以及导入依赖的类
package controllers
import play.api.libs.iteratee.Iteratee
import play.api.mvc._
import play.api.Logger
import play.api.libs.concurrent.Execution.Implicits.defaultContext
object ResponseTimeLogFilter extends EssentialFilter {
override def apply(next: EssentialAction): EssentialAction = new EssentialAction {
override def apply(v1: RequestHeader): Iteratee[Array[Byte], Result] = {
val startTime = System.currentTimeMillis()
next(v1).map { result =>
val endTime = System.currentTimeMillis()
val responseTime = (endTime - startTime) / 1000F
Logger.info(s"${v1.remoteAddress} - ${v1.method} ${v1.uri}"
+ s" ${result.header.status} ${responseTime}" + " sec")
result
}
}
}
}
示例5: AuthKey
//设置package包名称以及导入依赖的类
package jp.t2v.lab.play2.auth
import de.frosner.broccoli.conf
import de.frosner.broccoli.controllers.AuthConfigImpl
import de.frosner.broccoli.models.Anonymous
import de.frosner.broccoli.services.SecurityService
import de.frosner.broccoli.util.Logging
import jp.t2v.lab.play2.stackc.{RequestAttributeKey, RequestWithAttributes, StackableController}
import play.api.libs.iteratee.{Enumerator, Iteratee}
import scala.concurrent.ExecutionContext.Implicits.global
import play.api.mvc._
import scala.concurrent.Future
trait BroccoliWebsocketSecurity extends AsyncAuth with AuthConfigImpl with Logging {
self: Controller with AuthConfig =>
val securityService: SecurityService
private[auth] case object AuthKey extends RequestAttributeKey[User]
def withSecurity[A](req: RequestHeader)(
f: (Option[AuthenticityToken], User, RequestHeader) => (Iteratee[A, _], Enumerator[A]))
: Future[Either[Result, (Iteratee[A, _], Enumerator[A])]] =
securityService.authMode match {
case conf.AUTH_MODE_CONF =>
val maybeToken = extractToken(req)
val tokenString = maybeToken.getOrElse("<session ID missing>")
val maybeUser = restoreUser(req, scala.concurrent.ExecutionContext.Implicits.global)
maybeUser
.recover {
case exception =>
Logger.info(s"Authenticating the following session failed (session probably outdated): $tokenString") // TODO log level
(None, identity[Result] _) // don't follow IntelliJ's recommendation here!
}
.flatMap {
// TODO do we need the updater here? can we even use cookies or should we authenticate for each new WS connection?
case (Some(user), updater) =>
Logger.info(s"Successfully authenticated session $tokenString of $user") // TODO log level
Future.successful(Right(f(maybeToken, user, req)))
case (None, _) =>
Logger.info(s"Websocket to ${req.remoteAddress} not established because of missing authentication") // TODO log level
authenticationFailed(req).map(result => Left(result))
}
case conf.AUTH_MODE_NONE =>
Future.successful(Right(f(None, Anonymous, req)))
case other => throw new IllegalStateException(s"Unrecognized auth mode: ${securityService.authMode}")
}
implicit def loggedIn(implicit req: RequestWithAttributes[_]): User = securityService.authMode match {
case conf.AUTH_MODE_CONF => req.get(AuthKey).get
case conf.AUTH_MODE_NONE => Anonymous.asInstanceOf[User]
case other => throw new IllegalStateException(s"Unrecognized auth mode: ${securityService.authMode}")
}
}