使用JS连接状态的WebSockets

I use Ratchet PHP websockets lib. When using terminal (telnet) it works on all ports I choose and setup. Have tried localhost, 127.0.0.1, and websockets.test When I use JS it's only in connection mode, and then fails.

When connecting via telnet, all messages gets prompted. But not JS.

Error message after a good while of waiting:

WebSocket connection to 'ws://localhost:8002/' failed: WebSocket opening handshake timed out

And the code I use to connect:

var host = "ws://localhost:8002"; 

try {
  var socket = new WebSocket(host);
   alert('WebSocket - status ' + socket.readyState);
    socket.onopen = function (msg) {
        alert('open');
        alert("Welcome - status " + this.readyState);

        if (this.readyState != 1)
        {
            reconnect();
        }
    };
    socket.onmessage = function (msg) {
      alert("Received: " + msg.data);
    };
    socket.onclose = function (msg) {
       alert("Disconnected - status " + this.readyState);
    };
} catch (ex) {
    alert(ex);

}
$("msg").focus();