使用JS处于连接状态的WebSockets

时间:2019-01-07 10:25:53

标签: javascript php websocket ratchet

我使用Ratchet PHP websockets lib。 使用终端(远程登录)时,它可以在我选择和设置的所有端口上使用。 尝试了localhost,127.0.0.1和websockets.test 当我使用JS时,它仅处于连接模式,然后失败。

通过telnet连接时,将提示所有消息。但不是JS。

等待一段时间后出现错误消息:

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

我用来连接的代码:

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();

0 个答案:

没有答案