Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

show java example for websockets with session

  • Loading branch information...
commit 13d5876791ef409e092e4a097f54247d851e17dc 1 parent 04bdb11
@schleichardt authored
View
4 README.md
@@ -1,4 +1,2 @@
-This is your new Play 2.1 application
-=====================================
-This file will be packaged with your application, when using `play dist`.
+http://stackoverflow.com/questions/12879547/playframework-2-websockets-and-session/12880311
View
28 app/controllers/Application.java
@@ -1,6 +1,7 @@
package controllers;
import play.*;
+import play.libs.F;
import play.mvc.*;
import views.html.*;
@@ -8,7 +9,32 @@
public class Application extends Controller {
public static Result index() {
+ session("username", "fake-user");
return ok(index.render("Your new application is ready."));
}
-
+
+ public static WebSocket<String> chat(){
+ final Http.Session session = session();
+ final String name = session("username");//better version
+
+ return new WebSocket<String>() {
+ public void onReady(WebSocket.In<String> in, WebSocket.Out<String> out) {
+ System.out.println("ready");
+ in.onMessage(new F.Callback<String>() {
+ public void invoke(String event) {
+ System.out.println(event);
+ }
+ });
+
+ in.onClose(new F.Callback0() {
+ public void invoke() {
+ System.out.println("Disconnected");
+ }
+ });
+
+ out.write("Hello " + session.get("username"));
+ out.write("Hello " + name.toUpperCase());
+ }
+ };
+ }
}
View
16 app/views/index.scala.html
@@ -1,7 +1,19 @@
@(message: String)
@main("Welcome to Play 2.1") {
-
- @play20.welcome(message, style = "Java")
+
+ <div id=output>
+
+ </div>
+ <script>
+ var wsUri = "ws://localhost:@(if(play.Play.isTest) "3333" else "9000")/chat";
+ var websocket = new WebSocket(wsUri);
+ websocket.onmessage = function(evt) {
+ var element = document.createElement("p");
+ element.setAttribute("class", "ws-output")
+ element.innerHTML = evt.data;
+ document.getElementById("output").appendChild(element);
+ };
+ </script>
}
View
1  conf/routes
@@ -4,6 +4,7 @@
# Home page
GET / controllers.Application.index()
+GET /chat controllers.Application.chat()
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
View
3  project/Build.scala
@@ -11,7 +11,8 @@ object ApplicationBuild extends Build {
// Add your project dependencies here,
javaCore,
javaJdbc,
- javaEbean
+ javaEbean,
+ "org.seleniumhq.selenium" % "selenium-java" % "2.35.0"
)
val main = play.Project(appName, appVersion, appDependencies).settings(
View
2  project/plugins.sbt
@@ -5,4 +5,4 @@ logLevel := Level.Warn
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
// Use the Play sbt plugin for Play projects
-addSbtPlugin("play" % "sbt-plugin" % "2.1.3")
+addSbtPlugin("play" % "sbt-plugin" % "2.1.4-RC1")//2.1.3 could not find the tests
View
44 test/ApplicationTest.java
@@ -1,44 +0,0 @@
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.codehaus.jackson.JsonNode;
-import org.junit.*;
-
-import play.mvc.*;
-import play.test.*;
-import play.data.DynamicForm;
-import play.data.validation.ValidationError;
-import play.data.validation.Constraints.RequiredValidator;
-import play.i18n.Lang;
-import play.libs.F;
-import play.libs.F.*;
-
-import static play.test.Helpers.*;
-import static org.fest.assertions.Assertions.*;
-
-
-/**
-*
-* Simple (JUnit) tests that can call all parts of a play app.
-* If you are interested in mocking a whole application, see the wiki for more details.
-*
-*/
-public class ApplicationTest {
-
- @Test
- public void simpleCheck() {
- int a = 1 + 1;
- assertThat(a).isEqualTo(2);
- }
-
- @Test
- public void renderTemplate() {
- Content html = views.html.index.render("Your new application is ready.");
- assertThat(contentType(html)).isEqualTo("text/html");
- assertThat(contentAsString(html)).contains("Your new application is ready.");
- }
-
-
-}
View
28 test/IntegrationTest.java
@@ -1,28 +0,0 @@
-import org.junit.*;
-
-import play.mvc.*;
-import play.test.*;
-import play.libs.F.*;
-
-import static play.test.Helpers.*;
-import static org.fest.assertions.Assertions.*;
-
-import static org.fluentlenium.core.filter.FilterConstructor.*;
-
-public class IntegrationTest {
-
- /**
- * add your integration test here
- * in this example we just check if the welcome page is being shown
- */
- @Test
- public void test() {
- running(testServer(3333, fakeApplication(inMemoryDatabase())), HTMLUNIT, new Callback<TestBrowser>() {
- public void invoke(TestBrowser browser) {
- browser.goTo("http://localhost:3333");
- assertThat(browser.pageSource()).contains("Your new application is ready.");
- }
- });
- }
-
-}
View
24 test/WsTest.java
@@ -0,0 +1,24 @@
+import org.junit.*;
+
+import play.test.*;
+import play.libs.F.*;
+
+import java.util.concurrent.TimeUnit;
+
+import static play.test.Helpers.*;
+import static org.fest.assertions.Assertions.*;
+
+public class WsTest {
+ @Test
+ public void testWebSocketsWithSession() {
+ running(testServer(3333, fakeApplication(inMemoryDatabase())), FIREFOX, new Callback<TestBrowser>() {
+ public void invoke(TestBrowser browser) {
+ browser.goTo("http://localhost:3333/");
+ final String selector = ".ws-output";
+ browser.await().atMost(10, TimeUnit.SECONDS).until(selector).hasSize(2);
+ assertThat(browser.$(selector, 0).getText()).isEqualTo("Hello fake-user");
+ assertThat(browser.$(selector, 1).getText()).isEqualTo("Hello FAKE-USER");
+ }
+ });
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.