Note that there are some explanatory texts on larger screens.

plurals
  1. POPlay Framework 2.1 websockets in Chrome
    primarykey
    data
    text
    <p>I can't seem to get websocket communication to work in the Play Framework version 2.1. </p> <p>I created a simple test that does nothing but send messages back and forth with a push of a button. All the code for it is below. But nothing shows up except for the button.</p> <p>Has anybody seen this problem or can someone tell me what I may be doing wrong in the code below?</p> <p>I am using the latest version of Chrome.</p> <p>Here is my simple setup.</p> <p><strong>In Application.java</strong></p> <pre><code>public static Result index() { return ok(index.render()); } public static WebSocket&lt;String&gt; sockHandler() { return new WebSocket&lt;String&gt;() { // called when the websocket is established public void onReady(WebSocket.In&lt;String&gt; in, WebSocket.Out&lt;String&gt; out) { // register a callback for processing instream events in.onMessage(new Callback&lt;String&gt;() { public void invoke(String event) { System.out.println(event); } }); // write out a greeting out.write("I'm contacting you regarding your recent websocket."); } }; } </code></pre> <p><strong>In Routes File</strong> GET / controllers.Application.index()</p> <pre><code># Map static resources from the /public folder to the /assets URL path GET /assets/*file controllers.Assets.at(path="/public", file) GET /greeter controllers.Application.sockHandler() </code></pre> <p><strong>In Index.Scala.html</strong></p> <pre><code>@main(null) { &lt;div class="greeting"&gt;&lt;/div&gt; &lt;button class="send"&gt;Send&lt;/button&gt; &lt;script type="text/javascript" charset="utf-8"&gt; $(function() { var WS = window['MozWebSocket'] ? MozWebSocket : WebSocket var sock = new WS("@routes.Application.sockHandler()") sock.onmessage = function(event) { $('.greeting').append(event.data) } $('button.send').click(function() { sock.send("I'm sending a message now.") }); }) &lt;/script&gt; </code></pre> <p>}</p> <p><strong>In Main.scala.html</strong></p> <pre><code>@(title: String)(content: Html) &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;@title&lt;/title&gt; &lt;link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")"&gt; &lt;link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")"&gt; &lt;script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; @content &lt;/body&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload