本文整理汇总了Scala中play.api.libs.EventSource类的典型用法代码示例。如果您正苦于以下问题:Scala EventSource类的具体用法?Scala EventSource怎么用?Scala EventSource使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了EventSource类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ScalaEventSourceController
//设置package包名称以及导入依赖的类
package controllers
import java.io.File
import play.Play
import java.nio.file.Paths
import javax.inject.{Inject, Singleton}
import play.api.http.ContentTypes
import play.api.libs.EventSource
import play.api.mvc._
@Singleton
class ScalaEventSourceController @Inject()(cc: ControllerComponents) extends AbstractController(cc) with ScalaTicker {
def index() = Action {
Ok(views.html.scalaeventsource())
}
def streamClock() = Action {
Ok.chunked(stringSource via EventSource.flow).as(ContentTypes.EVENT_STREAM)
}
def upload = Action(parse.multipartFormData) { request =>
request.body.file("picture").map { picture =>
var basePath = "/app/public/upload/"
var archivoTmp = new File(basePath)
if(!archivoTmp.exists()) {
basePath = Play.application().getFile("/public/upload/").getAbsolutePath() + File.separator;
}
val filename = picture.filename
val contentType = picture.contentType
picture.ref.moveTo(Paths.get(basePath + filename), replace = true)
reiniciarSimulacion();
Ok(views.html.scalaeventsource())
}.getOrElse {
Redirect(routes.ScalaEventSourceController.index).flashing(
"error" -> "Missing file")
}
}
}
开发者ID:juancamilogaviriaacosta,项目名称:proyecto-transmimetro-JuanGaviria-MauricioMontano,代码行数:45,代码来源:ScalaEventSourceController.scala
示例2: Application
//设置package包名称以及导入依赖的类
package controllers
import play.api._
import play.api.mvc._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import play.api.libs.concurrent.Akka
import play.api.Play.current
import models._
import play.api.libs.EventSource
import play.api.libs.iteratee._
class Application extends Controller {
val (out, channel) = Concurrent.broadcast[String]
val listener = Akka.system.actorSelection("akka://application/user/listener")
listener ! Start(channel)
def dashboard = Action {
Ok(views.html.dashboard())
}
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
def createNewUser = Action.async {
listener ! UserRegistration
Future.successful(Ok(""))
}
def sendMessage = Action.async {
listener ! NewMessage
Future.successful(Ok(""))
}
def stream = Action { implicit req =>
Ok.feed(out &> EventSource()).as("text/event-stream")
}
}
object Application extends Application
示例3: index
//设置package包名称以及导入依赖的类
package controllers
import javax.inject._
import play.api._
import play.api.libs.EventSource
import play.api.libs.iteratee.Concurrent
import play.api.libs.json.JsValue
import play.api.mvc._
val (chatOut, chatChannel) = Concurrent.broadcast[JsValue]
def index = Action { implicit request =>
Ok(views.html.index(routes.HomeController.chatFeed(), routes.HomeController.postMessage()))
}
def chatFeed = Action { request =>
println("Someone has connected" + request.remoteAddress)
Ok.chunked(chatOut
&> EventSource()
).as("text/event-stream")
}
def postMessage = Action(parse.json) { request =>
chatChannel.push(request.body)
Ok
}
}
示例4: MainController
//设置package包名称以及导入依赖的类
package controllers
import java.util.UUID
import actors.EventStreamActor
import akka.actor.ActorSystem
import akka.stream.Materializer
import akka.stream.actor.ActorPublisher
import akka.stream.scaladsl.{Sink, Source}
import model._
import play.api.libs.EventSource
import play.api.libs.json.JsValue
import play.api.mvc._
import security.{UserAuthAction, UserAwareAction, UserAwareRequest}
import services.{ConsumerAggregator, RewindService}
class MainController(userAuthAction: UserAuthAction,
userAwareAction: UserAwareAction,
actorSystem: ActorSystem,
consumerAggregator: ConsumerAggregator,
rewindService: RewindService,
mat: Materializer) extends Controller {
def serverEventStream = userAwareAction { request =>
implicit val materializer = mat
implicit val actorFactory = actorSystem
val maybeUser = request.user
val maybeUserId = maybeUser.map(_.userId)
val actorRef = actorFactory.actorOf(EventStreamActor.props,
EventStreamActor.name(maybeUserId))
val eventStorePublisher = Source.fromPublisher(ActorPublisher[JsValue](actorRef))
.runWith(Sink.asPublisher(fanout = true))
val source = Source.fromPublisher(eventStorePublisher)
Ok.chunked(source.via(EventSource.flow)).as("text/event-stream")
}
def rewind = Action {
request =>
rewindService.refreshState(); Ok
}
def index = userAwareAction { request =>
Ok(views.html.pages.react(buildNavData(request),
WebPageData("Home")))
}
def error500 = Action {
InternalServerError(views.html.errorPage())
}
private def buildNavData(request: UserAwareRequest[_]): NavigationData = {
NavigationData(request.user, isLoggedIn = request.user.isDefined)
}
}
示例5: hashtagLoad
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.Inject
import akka.actor.ActorSystem
import akka.stream.Materializer
import akka.stream.scaladsl.Flow
import com.datastax.driver.core.Session
import controllers.stages.LoadStage
import play.api.libs.EventSource
import play.api.mvc.{Action, Controller}
import services.utils.CassandraHelper._
import services.utils.{CassandraHelper, CassandraUri}
import services.{Hashtag, MyTwitterListener, Tweet}
import scala.concurrent.ExecutionContext
def hashtagLoad = Action {
val source = twitterListener.hashtagSource
val loadFlow = LoadStage.getFlow(loadHashtags)
//Flow stage using take, as the limit function throws an exception when threshold is exceeded
val filter = Flow[Set[Hashtag]].take(MAX_TWEETS)
val hashtagSource = source.via(filter).via(loadFlow)
Ok.chunked(hashtagSource.map(set ? set.mkString) via EventSource.flow)
}
private def loadHashtags(s: Session, hashtags: Set[Hashtag]) = {
for (hashTag ? hashtags) {
s.execute(s"UPDATE $HASHTAG_TABLE SET hashtag_count = hashtag_count + 1 WHERE hashtag='" + hashTag.name.replace("'", "") + "';")
}
}
}
示例6: ChatController
//设置package包名称以及导入依赖的类
package controllers
import models.{User, ChatRoom}
import play.api.data._
import play.api.data.Forms._
import play.api.libs.EventSource
import play.api.libs.iteratee.{Enumeratee, Concurrent}
import play.api.libs.json.JsValue
import play.api.mvc.{WebSocket, Action, Controller}
import play.api.libs.concurrent.Execution.Implicits._
object ChatController extends Controller {
def index = Action { implicit request =>
Ok(views.html.chat())
}
def chatWS(username: String) = WebSocket.tryAccept[JsValue] { request =>
ChatRoom.join(username).map{ io =>
Right(io)
}.recover{ case e => Left(Ok(e))}
}
def chatSSE(username: String) = Action.async { request =>
ChatRoom.join(username).map{ io =>
Ok.feed(io._2
&> Concurrent.buffer(50)
&> Enumeratee.onIterateeDone{ () =>
play.Logger.info(request.remoteAddress + " - SSE disconnected")
}
&> EventSource()).as("text/event-stream")
}.recover{ case e => BadRequest(e)}
}
val talkForm = Form(
tuple(
"username" -> nonEmptyText,
"msg" -> text
)
)
def talk = Action{ implicit request =>
talkForm.bindFromRequest.fold(
error => BadRequest,
value => {
ChatRoom.talk(value._1, value._2)
Ok
}
)
}
}
示例7: HubController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.{Inject, Singleton}
import akka.stream.Materializer
import akka.stream.scaladsl.{BroadcastHub, Keep, MergeHub, Source}
import models.Message
import play.api.http.ContentTypes
import play.api.libs.EventSource
import play.api.mvc._
@Singleton
class HubController @Inject() (implicit val mat: Materializer) extends Controller {
private[this] val (sink, source) =
MergeHub.source[String](perProducerBufferSize = 16)
.toMat(BroadcastHub.sink(bufferSize = 256))(Keep.both)
.run()
def index = Action {
Ok(views.html.hub())
}
def receiveMessage = Action(BodyParsers.parse.json[Message]) { request =>
Source.single(request.body.toString).runWith(sink)
Ok
}
def sse = Action {
Ok.chunked(source via EventSource.flow).as(ContentTypes.EVENT_STREAM)
}
}
示例8: MainController
//设置package包名称以及导入依赖的类
package controllers
import play.api.mvc.{Action, Controller}
import process._
import play.api.libs.EventSource
import play.api.libs.iteratee._
class MainController extends Controller {
val (out, channel) = Concurrent.broadcast[String]
val dataReader = DataReader(channel)
dataReader.start()
def index() = Action {
Ok(views.html.index())
}
def classifiedTweets() = Action { implicit req =>
Ok.feed(out &> EventSource()).as("text/event-stream")
}
}
示例9: index
//设置package包名称以及导入依赖的类
package controllers
import javax.inject._
import play.api._
import play.api.mvc._
import twitter4j.conf.ConfigurationBuilder
import play.api.libs.iteratee.Enumeratee
import twitter4j._
import twitter4j.{Status => TwitterStatus}
import twitter4j.auth._
import twitter4j.conf._
import play.api.libs.json._
import scala.concurrent.ExecutionContext.Implicits.global
import services._
import play.api.libs.EventSource
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
def stream(query: String) = Action {
val queries = query.split(",")
val streams = queries.map { query =>
val twitterListener = new TwitterStreamListener(query, config)
twitterListener.listenAndStream
}
val mixStreams = streams.reduce((s1, s2) => s1 interleave s2)
val jsonMixStreams = mixStreams through toJson
Ok.chunked(jsonMixStreams through EventSource()).as("text/event-stream")
}
}
示例10: ChatApplication
//设置package包名称以及导入依赖的类
package controllers
import play.api.mvc._
import play.api.libs.json.JsValue
import play.api.libs.iteratee.{Concurrent, Enumeratee}
import play.api.libs.EventSource
import play.api.libs.concurrent.Execution.Implicits._
object ChatApplication extends Controller {
def chatFeed(room: String) = Action { req =>
println(req.remoteAddress + " - SSE connected")
Ok.feed(chatOut
&> filter(room)
&> Concurrent.buffer(50)
&> connDeathWatch(req.remoteAddress)
&> EventSource()
).as("text/event-stream")
}
}
示例11: StreamController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.{Inject, Named}
import actors.ScoreStreamer
import actors.StreamActor.Start
import actors.TournamentEventActor.SetChannel
import akka.actor.ActorRef
import akka.util.Timeout
import play.api.libs.json.JsObject
import play.api.mvc.{Action, Controller}
import scala.concurrent.duration._
import akka.stream.scaladsl.Source
import play.api.libs.EventSource
import play.api.libs.iteratee.{Concurrent, Enumerator}
import play.api.libs.streams.Streams
import scala.concurrent.ExecutionContext.Implicits.global
class StreamController @Inject()(@Named("score-streamer") scoreStreamer: ActorRef, @Named("stream-actor") streamActor: ActorRef, @Named("tournament-event-actor") tournamentEventActor: ActorRef) extends Controller {
implicit val timeout = Timeout(5 seconds)
val (out, channel) = Concurrent.broadcast[JsObject]
streamActor ! Start(channel)
val (scoreOut, scoreChanel) = Concurrent.broadcast[JsObject]
scoreStreamer ! ScoreStreamer.Start(scoreChanel)
tournamentEventActor ! SetChannel(streamActor)
def tournamentEventStream = Action { implicit req => enumeratorToStream(out)}
def scoreEventStream = Action { implicit req => enumeratorToStream(scoreOut)}
private def enumeratorToStream(enumerator: Enumerator[JsObject]) = {
val source: Source[String, Any] = Source.fromPublisher(Streams.enumeratorToPublisher(enumerator.map(x => x.toString())))
Ok.chunked(source via EventSource.flow).as("text/event-stream")
}
}