Php stream#u socket#u server赢了';不工作,但你创造工作

Php stream#u socket#u server赢了';不工作,但你创造工作,php,websocket,Php,Websocket,我正在尝试在php5+和win7x64环境中使用websocket。 首先,我使用stream_socket_server通过示例创建一个服务器套接字(另存为test_socket1.php) 并运行命令:php test_socket1.php,获得“Server created”输出。 之后,我在chrome/IE中运行client.html,它将进入stream_select,直到超时才返回。 因此,我使用另一个示例,即使用socket_create(另存为test_socket2.php

我正在尝试在php5+和win7x64环境中使用websocket。 首先,我使用stream_socket_server通过示例创建一个服务器套接字(另存为test_socket1.php)

并运行命令:php test_socket1.php,获得“Server created”输出。 之后,我在chrome/IE中运行client.html,它将进入stream_select,直到超时才返回。 因此,我使用另一个示例,即使用socket_create(另存为test_socket2.php)

并运行命令:php test_socket2.php,获得“服务器启动…”输出。然后我运行相同的client.html,工作正常。为什么这个能用? client.html如下所示

var host = "ws://127.0.0.1:8000"; // SET THIS TO YOUR SERVER
try {
    socket = new WebSocket(host);
    log('WebSocket - status '+socket.readyState);
    socket.onopen    = function(msg) { 
                           log("Welcome - status "+this.readyState); 
                       };
    socket.onmessage = function(msg) { 
                           log("Received: "+msg.data); 
                       };
    socket.onclose   = function(msg) { 
                           log("Disconnected - status "+this.readyState); 
                       };
}
catch(ex){ 
    log(ex); 
}
我还在test_socket1中添加了一些检查点,试图找出问题,但是运气不好,并且有两个未使用的结果

  • @stream_select-->不会返回任何内容,但client.html有“到'ws://127.0.0.1:8000'的WebSocket连接失败:WebSocket打开握手超时”响应
  • 将“@stream\u select”更改为“@socket\u select”(我知道这很愚蠢),然后它没有得到零返回,但从“stream\u socket\u accept”、“警告:stream\u socket\u accept():accept failed:connection timeout…”和client.html中得到了错误“与ws://127.0.0.1:8000的WebSocket连接失败:HTTP身份验证失败;“没有可用的有效凭据”,看起来服务器正在等待客户端的响应,而客户端正在等待服务器的握手
  • 所以我的问题是为什么这两个有不同的结果?stream_socket需要更多预配置的东西吗?谢谢

    ....    
    $this->master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)  or die("Failed: socket_create()");
    socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1) or die("Failed: socket_option()");
    socket_bind($this->master, '127.0.0.1', '8000')                      or die("Failed: socket_bind()");
    socket_listen($this->master,20)                               or die("Failed: socket_listen()");
    $this->sockets['m'] = $this->master;
    $this->stdout("Server started\nMaster socket: ".$this->master);
    ....
    
    var host = "ws://127.0.0.1:8000"; // SET THIS TO YOUR SERVER
    try {
        socket = new WebSocket(host);
        log('WebSocket - status '+socket.readyState);
        socket.onopen    = function(msg) { 
                               log("Welcome - status "+this.readyState); 
                           };
        socket.onmessage = function(msg) { 
                               log("Received: "+msg.data); 
                           };
        socket.onclose   = function(msg) { 
                               log("Disconnected - status "+this.readyState); 
                           };
    }
    catch(ex){ 
        log(ex); 
    }