Skip to content

Commit

Permalink
Merge pull request #662 from ihostage/fix-play-scala-streaming-example
Browse files Browse the repository at this point in the history
Fix `play-scala-streaming-example`
  • Loading branch information
mkurz authored Jun 13, 2024
2 parents 31d7ba7 + 1b64094 commit 4368165
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package controllers

import javax.inject.Inject

import play.api.mvc.{AbstractController, ControllerComponents}
import play.api.mvc.{AbstractController, Action, AnyContent, ControllerComponents, RequestHeader}

class HomeController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {

def index() = Action {
def index(): Action[AnyContent] = Action { implicit request =>
Ok(views.html.index())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package controllers

import javax.inject.{Inject, Singleton}

import org.apache.pekko.stream.Materializer
import play.api.http.ContentTypes
import play.api.libs.Comet
import play.api.mvc._
import views.html.helper.CSPNonce

import javax.inject.{Inject, Singleton}

@Singleton
class ScalaCometController @Inject() (cc: ControllerComponents, materializer: Materializer) extends AbstractController(cc)
class ScalaCometController @Inject()(cc: ControllerComponents, materializer: Materializer) extends AbstractController(cc)
with ScalaTicker {

def index() = Action {
def index(): Action[AnyContent] = Action { implicit request =>
Ok(views.html.scalacomet())
}

def streamClock() = Action {
Ok.chunked(stringSource via Comet.string("parent.clockChanged")).as(ContentTypes.HTML)
def streamClock(): Action[AnyContent] = Action { implicit request =>
Ok.chunked(stringSource.via(Comet.string("parent.clockChanged", CSPNonce()))).as(ContentTypes.HTML)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import play.api.mvc._
@Singleton
class ScalaEventSourceController @Inject()(cc: ControllerComponents) extends AbstractController(cc) with ScalaTicker {

def index() = Action {
def index(): Action[AnyContent] = Action { implicit request =>
Ok(views.html.scalaeventsource())
}

def streamClock() = Action {
def streamClock(): Action[AnyContent] = Action { implicit request =>
Ok.chunked(stringSource via EventSource.flow).as(ContentTypes.EVENT_STREAM)
}

Expand Down
2 changes: 1 addition & 1 deletion play-scala-streaming-example/app/views/index.scala.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@()
@()(implicit request: RequestHeader)

@main {

Expand Down
4 changes: 2 additions & 2 deletions play-scala-streaming-example/app/views/main.scala.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@(content: Html)
@(content: Html)(implicit request: RequestHeader)

<!DOCTYPE html>

Expand All @@ -7,7 +7,7 @@
<title>EventSource clock</title>
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
<script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
<script @{CSPNonce.attr} src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
</head>
<body>
@content
Expand Down
6 changes: 4 additions & 2 deletions play-scala-streaming-example/app/views/scalacomet.scala.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@()(implicit request: RequestHeader)

@main {

<h1>Comet clock</h1>
Expand All @@ -8,13 +10,13 @@ <h1 id="clock"></h1>
Clock events are pushed from the Server using a Comet connection.
</p>

<script type="text/javascript" charset="utf-8">
<script @{CSPNonce.attr} type="text/javascript" charset="utf-8">
// Called for each Comet message
var clockChanged = function(time) {
$('#clock').html(time.replace(/(\d)/g, '<span>$1</span>'))
}
</script>

<iframe id="comet" src="@routes.ScalaCometController.streamClock().unique"></iframe>
<iframe id="comet" hidden src="@routes.ScalaCometController.streamClock().unique"></iframe>

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@()(implicit request: RequestHeader)

@main {

<h1>Server Sent Event clock</h1>
Expand All @@ -8,7 +10,7 @@ <h1 id="clock"></h1>
Clock events are pushed from the Server using a Server Sent Event connection.
</p>

<script type="text/javascript" charset="utf-8">
<script @{CSPNonce.attr} type="text/javascript" charset="utf-8">

if (!!window.EventSource) {
var stringSource = new EventSource("@routes.ScalaEventSourceController.streamClock()");
Expand Down
4 changes: 4 additions & 0 deletions play-scala-streaming-example/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ lazy val root = (project in file("."))
"-Werror"
)
)

TwirlKeys.templateImports ++= Seq(
"views.html.helper.CSPNonce"
)

0 comments on commit 4368165

Please sign in to comment.