title: Java server JavaScript client WebSockets
body: <p>I'm trying to do a connection between a server in Java and a JavaScript client but I'm getting this error on client side: </p>
  
  <blockquote>
    <p><strong>WebSocket connection to 'ws://127.0.0.1:4444/' failed: Connection closed before receiving a handshake response</strong></p>
  </blockquote>
  
  <p>It maybe stays on OPENNING state because the <code>connection.onopen</code> function is never called. The <code>console.log('Connected!')</code> isn't being called.</p>
  
  <p>Could someone let me know what is going wrong here?</p>
  
  <p><strong>Server</strong></p>
  
  <pre><code>import java.io.IOException;
  import java.net.ServerSocket;
  
  public class Server {
  
      public static void main(String[] args) throws IOException {
  
          try (ServerSocket serverSocket = new ServerSocket(4444)) {
              GameProtocol gp = new GameProtocol();
  
              ServerThread player= new ServerThread(serverSocket.accept(), gp);
              player.start();
  
          } catch (IOException e) {
              System.out.println("Could not listen on port: 4444");
              System.exit(-1);
          }
  
      }
  
  }
  </code></pre>
  
  <p><strong>ServerThread</strong></p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.Socket;
  
  public class ServerThread extends Thread{
  
      private Socket socket = null;
      private GameProtocol gp;
  
      public ServerThread(Socket socket, GameProtocol gp) {
          super("ServerThread");
          this.socket = socket;
          this.gp = gp;
      }
  
      public void run() {
  
          try (
                  PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                  BufferedReader in = new BufferedReader(
                          new InputStreamReader(
                                  socket.getInputStream()));
                  ) {
              String inputLine, outputLine;
  
              while ((inputLine = in.readLine()) != null) {
                  outputLine = gp.processInput(inputLine);
                  System.out.println(outputLine);
              }
              socket.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p><strong>GameProtocol</strong></p>
  
  <pre><code>public class GameProtocol {
  
      public String processInput(String theInput) {
  
          String theOutput = null;
  
          theOutput = theInput;
  
          return theOutput;
      }
  }
  </code></pre>
  
  <p><strong>Client</strong></p>
  
  <pre><code>var connection = new WebSocket('ws://127.0.0.1:4444');
  
  connection.onopen = function () {
      console.log('Connected!');
      connection.send('Ping'); // Send the message 'Ping' to the server
  };
  
  // Log errors
  connection.onerror = function (error) {
      console.log('WebSocket Error ' + error);
  };
  
  // Log messages from the server
  connection.onmessage = function (e) {
      console.log('Server: ' + e.data);
  };
  </code></pre>