我正尝试在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不是有效的套接字资源“
我记得几个月前我让它运行了几次刷新页面,但现在不可能了。此外,我需要它一直工作,而不是仅仅在我刷新页面20次之后。
我也尝试过websocket.class.php文件,但这次在客户端得到了一个错误。浏览器现在返回“WebSocket握手时出错:'Sec-WebSocket-Accept‘报头缺失”。
所以,我不能让它与旧的或新的文件,与远程或本地服务器,魔法或占卜板!
有什么想法吗?
谢谢
发布于 2011-11-06 12:13:46
从最新的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;
}https://stackoverflow.com/questions/7238106
复制相似问题