2011-08-30 35 views
1

我正在尝试使用phpwebsocket库http://code.google.com/p/phpwebsocket/ 我使用server.php文件的r8版本。对于测试,我刚刚尝试了由网站提供的client.html文件。错误“不是一个有效的套接字资源”实现phpwebsocket库

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

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不是有效的套接字资源”

在浏览器中,我一收到“断开连接”消息该文件被加载。

我试图使服务器在本地使用XAMPP(Apache和PHP)工作,但我得到了同样的错误。我也试图改变港口,并按照这一问题的说明:

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

但我仍然得到错误“5是不是一个有效的套接字资源”

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

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

所以,我不能使它与远程或本地服务器,旧的或新的文件,魔术或ouija板工作!

有什么想法?

感谢

回答

2

与最新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; 
} 
相关问题