Skip to content

Commit

Permalink
Merge pull request #673 from playframework/mergify/bp/2.9.x/pr-669
Browse files Browse the repository at this point in the history
[2.9.x] websocket example: sync code of java example with the scala one (backport #669) by @mkurz
  • Loading branch information
mergify[bot] authored Jun 20, 2024
2 parents b04599e + b1bee28 commit c0c68ef
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 41 deletions.
7 changes: 2 additions & 5 deletions play-java-websocket-example/app/assets/stylesheets/main.less
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ body {
backface-visibility: hidden;
}

.details-holder {
z-index: 1;
transform-style: preserve-3d;
}

.chart-holder {
z-index: 2;
& p {
Expand All @@ -99,6 +94,8 @@ body {
.details-holder {
.transform(180deg);
text-align: center;
z-index: 1;
transform-style: preserve-3d;
& h4 {
padding: 20px;
}
Expand Down
14 changes: 5 additions & 9 deletions play-java-websocket-example/app/views/index.scala.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
@(request: play.mvc.Http.Request, webJarsUtil: org.webjars.play.WebJarsUtil)
@(implicit request: play.mvc.Http.Request, webJarsUtil: org.webjars.play.WebJarsUtil)
<!DOCTYPE html>

<html>
<head>
<title>Reactive Stock News Dashboard</title>

<link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">


@webJarsUtil.locate("bootstrap.min.css").css()
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.min.css")">

@webJarsUtil.locate("jquery.min.js").script()
@webJarsUtil.locate("jquery.flot.js").script()
<script type='text/javascript' src='@routes.Assets.at("javascripts/index.js")'></script>
<link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
@webJarsUtil.locate("jquery.min.js").script(scala.collection.immutable.Map("nonce" -> CSPNonce()))
@webJarsUtil.locate("jquery.flot.js").script(scala.collection.immutable.Map("nonce" -> CSPNonce()))
<script @{CSPNonce.attr} type='text/javascript' src='@routes.Assets.at("javascripts/index.js")'></script>
</head>
<body data-ws-url="@routes.HomeController.ws.webSocketURL(request)">
<div class="navbar navbar-inverse navbar-fixed-top">
Expand Down
15 changes: 8 additions & 7 deletions play-java-websocket-example/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ lazy val root = (project in file("."))
//.enablePlugins(PlayNettyServer).disablePlugins(PlayAkkaHttpServer) // uncomment to use the Netty backend
.settings(
name := "play-java-websocket-example",
version := "1.0",
version := "1.0-SNAPSHOT",
crossScalaVersions := Seq("2.13.14", "3.3.3"),
scalaVersion := crossScalaVersions.value.head,
// https://github.com/sbt/junit-interface
testOptions += Tests.Argument(TestFrameworks.JUnit, "-a", "-v"),
libraryDependencies ++= Seq(
guice,
ws,
"org.webjars" %% "webjars-play" % "2.9.1",
"org.webjars" % "bootstrap" % "2.3.2",
"org.webjars" % "flot" % "0.8.3",

// Testing libraries for dealing with CompletionStage...
"org.webjars" % "flot" % "0.8.3-1",
"org.webjars" % "bootstrap" % "3.3.7-1",
"org.assertj" % "assertj-core" % "3.25.3" % Test,
"org.awaitility" % "awaitility" % "4.2.1" % Test,
),
TwirlKeys.templateImports ++= Seq(
"views.html.helper.CSPNonce"
),
LessKeys.compress := true,
// https://github.com/sbt/junit-interface
testOptions += Tests.Argument(TestFrameworks.JUnit, "-a", "-v"),
javacOptions ++= Seq(
"-Xlint:unchecked",
"-Xlint:deprecation",
Expand Down
13 changes: 5 additions & 8 deletions play-java-websocket-example/conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ akka {
#}
}

# https://www.playframework.com/documentation/latest/SecurityHeaders
# Allow URLs from the same origin to be loaded by frames and scripts
play.filters.headers {
frameOptions = "SAMEORIGIN"
}
play.filters.enabled += play.filters.csp.CSPFilter

play.filters.csp.directives {
connect-src = "'self'"
Expand All @@ -32,7 +28,8 @@ play.filters.hosts {
allowed = ["localhost:9000"]
}

default.stocks=["GOOG", "AAPL", "ORCL"]
default.stocks = ["GOOG", "AAPL", "ORCL"]

sentiment.url = "http://text-processing.com/api/sentiment/"

sentiment.url="http://text-processing.com/api/sentiment/"
tweet.url="http://twitter-search-proxy.herokuapp.com/search/tweets"
tweet.url = "http://twitter-search-proxy.herokuapp.com/search/tweets"
4 changes: 3 additions & 1 deletion play-java-websocket-example/conf/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@

<logger name="play" level="INFO"/>

<!-- actors logging -->
<logger name="akka" level="INFO"/>
<logger name="akka.stream.Log" level="INFO"/>
<logger name="actors" level="DEBUG"/>

<logger name="actors" level="INFO"/>
<!-- controllers -->
<logger name="controllers" level="INFO"/>

<root level="INFO">
Expand Down
15 changes: 10 additions & 5 deletions play-java-websocket-example/test/controllers/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
import play.shaded.ahc.org.asynchttpclient.netty.ws.NettyWebSocket;
import play.shaded.ahc.org.asynchttpclient.ws.WebSocket;
import play.shaded.ahc.org.asynchttpclient.ws.WebSocketListener;

import play.shaded.ahc.org.asynchttpclient.ws.WebSocketUpgradeHandler;
import org.slf4j.Logger;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;

/**
* A quick wrapper around AHC WebSocket
*
* https://github.com/AsyncHttpClient/async-http-client/blob/2.0/client/src/main/java/org/asynchttpclient/ws/WebSocket.java
*/
public class WebSocketClient {

private AsyncHttpClient client;
Expand All @@ -21,11 +26,11 @@ public WebSocketClient(AsyncHttpClient c) {
this.client = c;
}

public CompletableFuture<NettyWebSocket> call(String url, String origin, WebSocketListener listener) throws ExecutionException, InterruptedException {
public CompletableFuture<NettyWebSocket> call(String url, String origin, WebSocketListener listener) {
final BoundRequestBuilder requestBuilder = client.prepareGet(url).addHeader("Origin", origin);

final WebSocketUpgradeHandler handler = new WebSocketUpgradeHandler.Builder().addWebSocketListener(listener).build();
final ListenableFuture<NettyWebSocket> future = requestBuilder.<NettyWebSocket>execute(handler);
final ListenableFuture<NettyWebSocket> future = requestBuilder.execute(handler);
return future.toCompletableFuture();
}

Expand Down Expand Up @@ -54,7 +59,7 @@ public void onClose(WebSocket webSocket, int i, String s) {
}

public void onError(Throwable t) {
// do nothing
//logger.error("onError: ", t);
throwableFound = t;
}

Expand All @@ -65,4 +70,4 @@ public void onTextFrame(String payload, boolean finalFragment, int rsv) {
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import akka.actor.typed.{ ActorRef, Scheduler }
import akka.actor.typed.scaladsl.AskPattern._
import akka.stream.scaladsl._
import akka.util.Timeout
import org.webjars.play.WebJarsUtil
import play.api.Logger
import play.api.libs.json._
import play.api.mvc._
Expand All @@ -19,7 +20,7 @@ import scala.concurrent.{ ExecutionContext, Future }
* This class creates the actions and the websocket needed.
*/
@Singleton
class HomeController @Inject()(userParentActor: ActorRef[UserParentActor.Create],
class HomeController @Inject()(userParentActor: ActorRef[UserParentActor.Create], webJarsUtil: org.webjars.play.WebJarsUtil,
cc: ControllerComponents)
(implicit ec: ExecutionContext, scheduler: Scheduler)
extends AbstractController(cc) with SameOriginCheck {
Expand All @@ -28,7 +29,7 @@ class HomeController @Inject()(userParentActor: ActorRef[UserParentActor.Create]

// Home page that renders template
def index = Action { implicit request: Request[AnyContent] =>
Ok(views.html.index())
Ok(views.html.index(webJarsUtil))
}

/**
Expand Down
8 changes: 4 additions & 4 deletions play-scala-websocket-example/app/views/index.scala.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
@()(implicit r: Request[_])
@(webJarsUtil: org.webjars.play.WebJarsUtil)(implicit r: Request[_])
<!DOCTYPE html>

<html>
<head>
<title>Reactive Stock News Dashboard</title>
<link rel='stylesheet' href='@routes.Assets.at("lib/bootstrap/css/bootstrap.min.css")'>
@webJarsUtil.locate("bootstrap.min.css").css()
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.min.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
<script @{CSPNonce.attr} type='text/javascript' src='@routes.Assets.at("lib/jquery/jquery.min.js")'></script>
<script @{CSPNonce.attr} type='text/javascript' src='@routes.Assets.at("lib/flot/jquery.flot.js")'></script>
@webJarsUtil.locate("jquery.min.js").script(scala.collection.immutable.Map("nonce" -> CSPNonce()))
@webJarsUtil.locate("jquery.flot.js").script(scala.collection.immutable.Map("nonce" -> CSPNonce()))
<script @{CSPNonce.attr} type='text/javascript' src='@routes.Assets.at("javascripts/index.js")'></script>
</head>
<body data-ws-url="@routes.HomeController.ws.webSocketURL()">
Expand Down
1 change: 1 addition & 0 deletions play-scala-websocket-example/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ lazy val root = (project in file("."))
libraryDependencies ++= Seq(
guice,
ws,
"org.webjars" %% "webjars-play" % "3.0.1",
"org.webjars" % "flot" % "0.8.3-1",
"org.webjars" % "bootstrap" % "3.3.7-1",
"org.scalatestplus.play" %% "scalatestplus-play" % "6.0.1" % Test,
Expand Down
2 changes: 2 additions & 0 deletions play-scala-websocket-example/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ GET /sentiment/:symbol controllers.StockSentiment.get(symbol)

# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)

-> /webjars webjars.Routes

0 comments on commit c0c68ef

Please sign in to comment.