【问题标题】:error "not a valid Socket resource" implementing the phpwebsocket library实现 phpwebsocket 库的错误“不是有效的 Socket 资源”
【发布时间】:2011-08-30 02:30:38
【问题描述】:

我正在尝试使用http://code.google.com/p/phpwebsocket/ 的 phpwebsocket 库 我正在使用 server.php 文件的 r8 版本。为了进行测试,我刚刚尝试使用同样由网站提供的 client.html 文件。

当服务器启动时,我得到这个:

Server Started : 2011-08-29 22:11:23
Master socket  : Resource id #4
Listening on   : www.midomain.com port 12345

但是当我在浏览器中加载client.html文件时,服务器显示如下错误:

Notice: Undefined variable: key1 in /home/mink500/public_html/test/server.php on line 143
Notice: Undefined variable: key2 in /home/mink500/public_html/test/server.php on line 143
Warning: socket_select(): 5 is not a valid Socket resource in /home/mink500/public_html/test/server.php on line 15

有两个变量没有定义,函数socket_select()返回错误“5 is not a valid Socket resource”

在浏览器中,一旦加载文件,我就会收到“已断开连接”消息。

我尝试使用 XAMPP(Apache 和 PHP)使服务器在本地工作,但我遇到了同样的错误。我还尝试更改端口并按照此问题中的说明进行操作:

http://code.google.com/p/phpwebsocket/issues/detail?id=33

但我仍然收到错误“5 不是有效的 Socket 资源”

我记得几个月前我让它运行了几次刷新页面,但现在不可能了。此外,我需要它一直工作,而不仅仅是在我刷新页面 20 次之后。

我也尝试过使用 websocket.class.php 文件,但这次我在客户端收到错误。浏览器现在返回“WebSocket 握手期间出错:缺少 'Sec-WebSocket-Accept' 标头”。

所以,我无法让它与旧文件或新文件、远程或本地服务器、魔法或占卜板一起工作!

有什么想法吗?

谢谢

【问题讨论】:

    标签: phpwebsocket


    【解决方案1】:

    从最新的 phpwebsocket client.html 和 server.php 文件开始,我用下面的代码替换了 getheaders() 和 dohandshake() 函数,在最新的 Chrome 中对我有用。但是,它目前不会写入浏览器,也不会在用户在聊天框中发表评论后保持活动状态。

    function dohandshake($user, $buffer) {
    $key = null;
    
    console("\nRequesting handshake...");
    console($buffer);
    console("Handshaking...");
    
    preg_match("#Sec-WebSocket-Key: (.*?)\r\n#", $buffer, $match) && $key = $match[1];
    
    $key .= "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    $key = sha1($key);
    $key = pack('H*', $key);
    $key = base64_encode($key);
    
    $upgrade =
            "HTTP/1.1 101 Switching Protocols\r\n" .
            "Upgrade: websocket\r\n" .
            "Connection: Upgrade\r\n" .
            "Sec-WebSocket-Accept: {$key}\r\n\r\n";
    
    socket_write($user->socket, $upgrade . chr(0), strlen($upgrade . chr(0)));
    $user->handshake = true;
    console($upgrade);
    console("Done handshaking...");
    return true;
    }
    
    function getheaders($header) {
    $retVal = array();
    $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
    foreach ($fields as $field) {
        if (preg_match('/([^:]+): (.+)/m', $field, $match)) {
            $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
            if (isset($retVal[$match[1]])) {
                $retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
            } else {
                $retVal[$match[1]] = trim($match[2]);
            }
        }
    }
    
    if (preg_match("/GET (.*) HTTP/", $header, $match)) {
        $retVal['GET'] = $match[1];
    }
    return $retVal;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-16
      • 2020-01-05
      • 2017-04-10
      • 1970-01-01
      • 2019-08-03
      • 1970-01-01
      • 2017-08-30
      • 2012-06-21
      • 1970-01-01
      相关资源
      最近更新 更多