title: How to use WebSockets with Play Framework?
body: <p>I have downloaded Play Framework from GitHub and compiled it. Now I want to use WebSockets and made a JavaScript client and a WebSocket controller similar to the one on <a href="http://www.playframework.org/documentation/1.2.3/asynchronous#UsingWebSockets" rel="noreferrer">Using WebSockets</a>, but it doesn't work. I can open the WebSocket, but the controller doesn't receive any message I send to it. And I can't close the WebSocket with <code>ws.close();</code>, but if I update my webpage in the browser, the WebSocket is closed on the server.</p>
  
  <p>How can I receive and send WebSocket messages using Play Framework?</p>
  
  <p>Here is my Play Framework WebSocketController:</p>
  
  <pre><code>public class TestSocket extends WebSocketController {
  
      public static void hello(String name) {
  
          while(inbound.isOpen()) {
              WebSocketEvent evt = await(inbound.nextEvent());
              if(evt instanceof WebSocketFrame) {
                  WebSocketFrame frame = (WebSocketFrame)evt;
                  System.out.println("received: " + frame.getTextData());
                  if(!frame.isBinary()) {
                      if(frame.getTextData().equals("quit")) {
                          outbound.send("Bye!");
                          disconnect();
                      } else {
                          outbound.send("Echo: %s", frame.getTextData());
                      }
                  }
              } else if(evt instanceof WebSocketClose) {
                  System.out.println("socket closed");
              }
          }
      }
  
  }
  </code></pre>
  
  <p>Here is my JavaScript client: </p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8"/&gt;
  &lt;title&gt;WebSocket test&lt;/title&gt;
  &lt;style&gt;
  .message {background: lightgray;}
  &lt;/style&gt;
  &lt;script&gt;
  window.onload = function() {
      document.getElementById('sendbutton')
          .addEventListener('click', sendMessage, false);
      document.getElementById('connectbutton')
          .addEventListener('click', connect, false);
      document.getElementById('disconnectbutton')
          .addEventListener('click', disconnect, false);
  }
  
  function writeStatus(message) {
      var html = document.createElement("div");
      html.setAttribute('class', 'message');
      html.innerHTML = message;
      document.getElementById("status").appendChild(html);
  }
  
  function connect() {
      ws = new WebSocket("ws://localhost:9000/ws?name=TestUser");
  
      ws.onopen = function(evt) { 
          writeStatus("connected");
      }
  
      ws.onclose = function(evt) {
          writeStatus("disconnected");
      }
  
      ws.onmessage = function(evt) {
          writeStatus("response: " + evt.data);
      }
  
      ws.onerror = function(evt) {
          writeStatus("error: " + evt.data);
      }
  }
  
  function disconnect() {
      ws.close();
  }
  
  function sendMessage() {
      ws.send(document.getElementById('messagefield').value);
  }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;h1&gt;WebSocket test&lt;/h1&gt;
  &lt;button id="connectbutton"&gt;Connect&lt;/button&gt;
  &lt;button id="disconnectbutton"&gt;Disconnect&lt;/button&gt;&lt;br&gt;
  &lt;input type="text" id="messagefield"/&gt;&lt;button id="sendbutton"&gt;Send&lt;/button&gt;
  &lt;div id="status"&gt;&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>