本文整理汇总了Scala中play.api.libs.streams.ActorFlow类的典型用法代码示例。如果您正苦于以下问题:Scala ActorFlow类的具体用法?Scala ActorFlow怎么用?Scala ActorFlow使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ActorFlow类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: MyApplicationController
//设置package包名称以及导入依赖的类
package my.samples.controllers
import play.api.mvc._
import my.samples.core.AppBindings
import play.api.libs.json.JsValue
import play.api.libs.streams.ActorFlow
import play.api.mvc.WebSocket.MessageFlowTransformer
final class MyApplicationController(bindings: AppBindings) extends Controller {
implicit val messageFlowTransformer =
MessageFlowTransformer.jsonMessageFlowTransformer[JsValue, JsValue]
implicit val actorSystem = bindings.actorSystem
implicit val materializer = bindings.materializer
def home = Action { implicit request =>
Ok("The API is ready")
}
def observable = WebSocket.accept[JsValue, JsValue] { request =>
ActorFlow.actorRef(out => MyWebSocketActor.props(bindings.globalChannel.publishChannel, out))
}
def connectableObservable = WebSocket.accept[JsValue, JsValue] { request =>
ActorFlow.actorRef(out => MyWebSocketActor.props(bindings.globalChannel.publishChannel, out))
}
}
示例2: WebSocketController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject._
import akka.actor.{ActorSystem, _}
import akka.stream.Materializer
import play.api.libs.json._
import play.api.libs.streams.ActorFlow
import play.api.mvc._
import models._
import play.api.cache.{Cache, CacheApi}
@Singleton
class WebSocketController @Inject()(implicit system: ActorSystem, materializer: Materializer, cache: CacheApi) {
object MyWebSocketActor {
def props(out: ActorRef) = Props(new MyWebSocketActor(out))
}
class MyWebSocketActor(out: ActorRef) extends Actor {
override def preStart() {
println("open")
println(self.path.parent.toSerializationFormat)
val x = system.actorSelection(self.path.parent.toSerializationFormat)
x ! "Message12"
}
override def postStop() {
println("close")
}
override def receive = {
case request: JsValue =>
val response = handleMessage(request)
out ! response
}
def handleMessage(event: Event): JsValue = {
event match {
case event: EventA => {
val actorId = self.path.parent.toSerializationFormat
cache.set("actor-id_" + event.data, actorId)
Json.toJson(event)
}
case event: EventB => Json.toJson(event)
}
}
}
def socket = WebSocket.accept[JsValue, JsValue] { request =>
ActorFlow.actorRef(out => MyWebSocketActor.props(out))
}
}
示例3: DashboardController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.{Inject, Singleton}
import akka.actor.ActorSystem
import akka.stream.Materializer
import org.apache.spark.streaming.Seconds
import play.api.Configuration
import play.api.inject.ApplicationLifecycle
import play.api.libs.streams.ActorFlow
import play.api.mvc._
import services.{SparkService, StreamActor}
@Singleton()
class DashboardController @Inject() ()(implicit system: ActorSystem, materializer: Materializer, configuration : Configuration, lifecycle: ApplicationLifecycle) extends Controller {
val sparkService = SparkService.getInstance(configuration, lifecycle)
def index = Action {
Ok(views.html.index())
}
def stream = WebSocket.accept[String, String] { request =>
val filters = Seq("Euro")
val tweets = sparkService.getTwitterStream(filters)
val stream = tweets
// .map { tweet =>
// tweet.getText
// }
// .flatMap { text => text.split("\\s") }
// .filter(_.startsWith("#"))
.map { tweet =>
Option(tweet.getPlace).map(_.getCountry)
}
.filter(_.isDefined)
.map(_.get)
.map { element => (element, 1)}
.reduceByKeyAndWindow(_ + _, Seconds(60))
.transform( count => count.sortBy(_._2, false))
ActorFlow.actorRef(out => StreamActor.props(out, stream))
}
}
示例4: VoiceController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.{Inject, Named, Singleton}
import actors.{FeedbackAggregatorActor, MonkeyActor, UserWebSocketActor}
import akka.actor.{ActorRef, ActorSystem}
import akka.stream.Materializer
import play.api.libs.json.JsValue
import play.api.libs.streams.ActorFlow
import play.api.mvc._
@Singleton
class VoiceController @Inject()(@Named("feedbackActor") feedbackActor: ActorRef)
(implicit system: ActorSystem, materializer: Materializer) extends Controller {
// Render basic page
def index = Action { implicit request =>
Ok(views.html.voice.the_voice())
}
def openWebSocket = WebSocket.accept[JsValue, JsValue] { request =>
ActorFlow.actorRef(outActorRef => UserWebSocketActor.props(outActorRef, feedbackActor))
}
}
示例5: BasicActorCtrl
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.Inject
import akka.actor.ActorSystem
import akka.stream.Materializer
import com.mohiva.play.silhouette.api.{HandlerResult, Silhouette}
import models.daos.actors.ChatActorFactory
import play.api.Logger
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.json.JsValue
import play.api.libs.streams.ActorFlow
import play.api.mvc._
import utils.DefaultEnv
import scala.concurrent.Future
class BasicActorCtrl @Inject()(
val messagesApi: MessagesApi,
implicit val mat: Materializer,
implicit val actorSystem: ActorSystem,
val chatActorFactory: ChatActorFactory,
silhouette: Silhouette[DefaultEnv]
) extends Controller with I18nSupport {
val log = Logger("IDP." + this.getClass.getSimpleName);
def send(eventName: String, username: String, token: String) = WebSocket.acceptOrResult[JsValue, JsValue] {
request =>
{
//log.debug(s"new query string: ${request.getQueryString("X-Auth-Token")}")
val tmpRequest = request.copy(headers = new Headers(Seq("X-Auth-Token" -> token)))
implicit val req = Request(tmpRequest, AnyContentAsEmpty)
silhouette.SecuredRequestHandler { securedRequest =>
Future.successful(HandlerResult(Ok, Some(securedRequest.identity)))
}.map {
case HandlerResult(r, Some(user)) => {
Right(ActorFlow.actorRef(chatActorFactory.props(username, eventName, _)))
}
case HandlerResult(r, None) => {
Left(r)
}
}
}
}
}
示例6: ChatController
//设置package包名称以及导入依赖的类
package controllers.chat
import javax.inject.Inject
import akka.actor._
import akka.stream._
import akka.stream.scaladsl.{ Flow, Keep }
import domains.chat.ChatMessage
import play.api.libs.json.JsValue
import play.api.libs.streams.ActorFlow
import play.api.mvc.WebSocket
import services.chat.ChatService
class ChatController @Inject()(
implicit val system: ActorSystem,
implicit val materializer: Materializer,
streamChatService: ChatService
) {
def start(roomId: String) = WebSocket.accept[JsValue, JsValue] { request =>
val userName = request.queryString("user_name").headOption.getOrElse("anon")
val userInput: Flow[JsValue, ChatMessage, _] = ActorFlow.actorRef[JsValue, ChatMessage](out => ChatRequestActor.props(out, userName))
val room = streamChatService.start(roomId, userName)
val userOutPut: Flow[ChatMessage, JsValue, _] = ActorFlow.actorRef[ChatMessage, JsValue](out => ChatResponseActor.props(out,userName))
userInput.viaMat(room.bus)(Keep.right).viaMat(userOutPut)(Keep.right)
}
}
示例7: ClientMessagingController
//设置package包名称以及导入依赖的类
package entrypoint
import access.authentication.PasswordResetCodeSender
import access.registration.AccountActivationCodeSender
import access.{AuthenticatedActionCreator, JWTAlgorithmProvider, JWTPrivateKeyProvider, JWTPublicKeyProvider}
import akka.actor.ActorSystem
import akka.stream.Materializer
import com.eigenroute.id.UUIDProvider
import com.eigenroute.time.TimeProvider
import com.google.inject.Inject
import messaging.MessageTranslator
import play.api.Configuration
import play.api.libs.json.JsValue
import play.api.libs.streams.ActorFlow
import play.api.mvc._
class ClientMessagingController @Inject() (
override val timeProvider: TimeProvider,
override val configuration: Configuration,
override val authenticationAPI: AuthenticationAPI,
registrationAPI: RegistrationAPI,
override val jWTAlgorithmProvider: JWTAlgorithmProvider,
override val jWTPublicKeyProvider: JWTPublicKeyProvider,
jWTPrivateKeyProvider: JWTPrivateKeyProvider,
system: ActorSystem,
materializer: Materializer,
userChecker: UserChecker,
userAPI: UserAPI,
uUIDProvider: UUIDProvider,
passwordResetCodeSender: PasswordResetCodeSender,
accountActivationLinkSender:AccountActivationCodeSender)
extends Controller
with AuthenticatedActionCreator {
implicit val mat = materializer
implicit val actorRefFactory = system
def connect = {
WebSocket.accept[JsValue, JsValue] { request =>
ActorFlow.actorRef(unnamedClient =>
MessageTranslator.props(
userChecker,
userAPI,
authenticationAPI,
registrationAPI,
jWTAlgorithmProvider,
jWTPublicKeyProvider,
jWTPrivateKeyProvider,
configuration,
timeProvider,
uUIDProvider,
unnamedClient,
passwordResetCodeSender,
accountActivationLinkSender))
}
}
}
示例8: BidChampController
//设置package包名称以及导入依赖的类
package controllers
import java.util.UUID
import javax.inject._
import actors.WebSocketActor
import akka.actor.ActorSystem
import akka.stream.Materializer
import play.api.Logger
import play.api.libs.json.JsValue
import play.api.libs.streams.ActorFlow
import play.api.mvc._
import services.{BidChampCore, UserStore}
import scala.concurrent.Future
import scala.util.Try
@Singleton
class BidChampController @Inject()(
bidChamp: BidChampCore, val userStore: UserStore
)(implicit system: ActorSystem, materializer: Materializer)
extends Controller with Authorization {
val logger: Logger = Logger(this.getClass)
def state = Action { Ok("Cool story bro") }
def socket = WebSocket.acceptOrResult[JsValue, JsValue] { request =>
Future.successful {
val flow = for {
tokenString <- request.session.get("session-token")
token <- parseUuid(tokenString)
userAccount <- userStore.getUserByToken(token)
} yield ActorFlow.actorRef(out => WebSocketActor.props(userAccount.uuid, out, bidChamp.gameActor))
flow.toRight(Forbidden)
}
}
def parseUuid(s: String): Option[UUID] = Try(UUID.fromString(s)).toOption
}
示例9: SnakeController
//设置package包名称以及导入依赖的类
package controllers
import java.util.UUID
import javax.inject.Inject
import actors.{SnakeMasterActor, SnakeSocketActor}
import akka.actor.{ActorSystem, Props}
import akka.stream.Materializer
import play.api.libs.streams.ActorFlow
import play.api.mvc.{Controller, WebSocket}
import shared._
class SnakeController @Inject()(
actorSystem: ActorSystem,
materializer: Materializer
) extends Controller {
import models.Formats._
lazy val master = actorSystem.actorOf(Props(new SnakeMasterActor()))
def ws() = WebSocket.accept[Direction, WorldState] { rs =>
val clientId = UUID.randomUUID()
ActorFlow.actorRef(out => Props(new SnakeSocketActor(out, master, clientId)))(actorSystem, materializer)
}
}
示例10: WebSocketController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.Inject
import actors.{MessageActor, RoomActor, RoomClientActor, RoomMasterActor}
import akka.actor.ActorSystem
import akka.stream.Materializer
import json.{IncomingMessage, IncomingRoomMessage, OutgoingMessage, OutgoingRoomMessage}
import play.api.libs.json.Json
import play.api.libs.streams.ActorFlow
import play.api.mvc.WebSocket.MessageFlowTransformer
import play.api.mvc.{Controller, WebSocket}
class WebSocketController @Inject() (implicit system: ActorSystem, materializer: Materializer) extends Controller{
implicit val inMessageFormat = Json.format[IncomingMessage]
implicit val outMessageFormat = Json.format[OutgoingMessage]
implicit val inRoomMessageFormat = Json.format[IncomingRoomMessage]
implicit val outRoomMessageFormat = Json.format[OutgoingRoomMessage]
implicit val messageFlowTransformer = MessageFlowTransformer.jsonMessageFlowTransformer[IncomingMessage, OutgoingMessage]
implicit val roomFlowTransformer = MessageFlowTransformer.jsonMessageFlowTransformer[IncomingRoomMessage, OutgoingRoomMessage]
def messageWebSocket(userId: String) = WebSocket.accept[IncomingMessage, OutgoingMessage] {
request => ActorFlow.actorRef(out => MessageActor.props(out, userId))
}
def roomWebSocket(userId: String) = WebSocket.accept[IncomingRoomMessage, OutgoingRoomMessage] {
request => ActorFlow.actorRef(out => RoomClientActor.props(out, userId))
}
}
示例11: HomeController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject._
import akka.actor.ActorSystem
import akka.stream.Materializer
import play.api.libs.json._
import play.api.libs.streams.ActorFlow
import play.api.mvc._
import services.{DestinationsFeedWSConnection, KafkaProducerService}
@Singleton
class HomeController @Inject()(
implicit system: ActorSystem,
materializer: Materializer,
environment: play.api.Environment,
configuration: play.api.Configuration) extends Controller {
// TODO: Have this be injected
private val logQueue = KafkaProducerService()
lazy val destinationFeedManager = system.actorSelection("akka://application/user/DestinationsFeedManager")
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
def liveState = WebSocket.accept[JsValue, JsValue] { request =>
ActorFlow.actorRef(out => DestinationsFeedWSConnection.props(destinationFeedManager, out))
}
def cityPage(country: String, city: String) = Action { request =>
logQueue.publish("requestpathdata", request.path)
Ok(s"You requested $city in $country")
}
}
示例12: WebSocketController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.Singleton
import actors.{ActorManager, WebSocketActor}
import akka.actor.{ActorSystem, Props}
import akka.stream.Materializer
import com.google.inject.Inject
import play.api.Configuration
import play.api.libs.json.JsValue
import play.api.libs.streams.ActorFlow
import play.api.mvc.{Action, Controller, WebSocket}
import play.modules.reactivemongo.{MongoController, ReactiveMongoApi, ReactiveMongoComponents}
import scala.concurrent.ExecutionContext
@Singleton
class WebSocketController @Inject() (val reactiveMongoApi: ReactiveMongoApi)
(implicit executionContext: ExecutionContext,
configuration: Configuration,
system: ActorSystem,
materializer: Materializer)
extends Controller with MongoController with ReactiveMongoComponents {
lazy final val actorManager = system.actorOf(Props[ActorManager],"actor-manager")
def socket = WebSocket.accept[JsValue,JsValue] { request =>
//request.session.get(Id).map { id =>
ActorFlow.actorRef(out => WebSocketActor.props(actorManager,out,"10"))
//}.getOrElse(throw new Exception("connection error"))
}
def index = Action { implicit request =>
Ok(views.html.websocket("test"))
}
}
示例13: Application
//设置package包名称以及导入依赖的类
package controllers
import akka.actor.ActorSystem
import akka.stream.Materializer
import com.google.inject.Inject
import play.api.mvc._
import shared.SharedMessages
import play.api.libs.streams.ActorFlow
import sparkConn.{SparkCommon, TwitterStreamer}
import sparkStream._
class Application @Inject() (implicit system: ActorSystem,
materializer: Materializer) extends Controller {
def index = Action {
Ok(views.html.index(SharedMessages.itWorks))
}
def sparkSocket = WebSocket.accept[String,String] { request =>
ActorFlow.actorRef(out => WebSocketActor.props(out))
}
def setupLogging() = {
import org.apache.log4j.{Level,Logger}
val rootLogger = Logger.getRootLogger
rootLogger.setLevel(Level.ERROR)
}
def fontDir(file: String) = Action.async {
implicit request => {
controllers.Assets.at("/../client/src/main/resources/", file, false).apply(request)
}
}
def sparkBookData = Action {
val sc = SparkCommon.sc
//System.setProperty("twitter4j.oauth")
val inputList = sc.parallelize(List(1,2,3,4))
val text = "Test123" + inputList.collect().foldLeft(" ")((a,b) => a + b)
val input = sc.textFile("book.txt")
var words = input.flatMap(line => line.split(' '))
val lowerCaseWords = words.map(_.toLowerCase())
val wordCounts = lowerCaseWords.countByValue()
val sample = wordCounts.take(20)
for ((word, count) <- sample) {
println(word + " " + count)
}
//val ssc = SparkCommon.ssc
Ok(text)
}
}
示例14: Chat
//设置package包名称以及导入依赖的类
package controllers
import actors.{Chatroom, User}
import akka.actor.{ActorRef, ActorSystem}
import akka.stream.Materializer
import play.api.libs.json.JsValue
import play.api.libs.streams.ActorFlow
import play.api.mvc.{Action, Controller, WebSocket}
class Chat(implicit actorSystem: ActorSystem, materializer: Materializer)
extends Controller {
// long-lived chatroom
private val chatRoom: ActorRef = actorSystem.actorOf(Chatroom.props)
def index = Action { implicit req =>
Ok(views.html.index())
}
def chat = WebSocket.accept[JsValue, JsValue] { _ =>
// instantiate a User actor and pass it the chatroom
ActorFlow.actorRef(out => User.props(chatRoom, out))
}
}
示例15: GameController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject._
import actors.{HostWebSocketActor, ScorekeeperWebSocketActor, SignonWebSocketActor}
import akka.actor.ActorSystem
import akka.stream.Materializer
import play.api.libs.streams.ActorFlow
import play.api.mvc._
class GameController @Inject()(implicit system: ActorSystem,
materializer: Materializer,
webJarAssets: WebJarAssets)
extends Controller {
def signonSocket: WebSocket = WebSocket.accept[String, String] { request =>
ActorFlow.actorRef(out => SignonWebSocketActor.props(out))
}
def hostSocket: WebSocket = WebSocket.accept[String, String] { request =>
ActorFlow.actorRef(out => HostWebSocketActor.props(out))
}
def scorekeeperSocket: WebSocket = WebSocket.accept[String, String] { request =>
ActorFlow.actorRef(out => ScorekeeperWebSocketActor.props(out))
}
def index = Action { request =>
request.session.get("name").map { name =>
Ok(views.html.game("Let's play trivia!.", name, webJarAssets))
}.getOrElse(Redirect(routes.LoginController.loginIntro()))
}
}