错误“不是有效的 Socket 资源”实现 phpwebsocket 库

发布于 2024-12-01 19:09:14 字数 1343 浏览 0 评论 0原文

我正在尝试使用 phpwebsocket 库 http://code.google.com/p/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不是有效的套接字资源”

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

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

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

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

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

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

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

有什么想法吗?

谢谢

I'm trying to work with the phpwebsocket library at http://code.google.com/p/phpwebsocket/
I'm using the version r8 of the server.php file. For testing I just tried with the client.html file, also provided by the website.

When the server is started I get this:

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

But when I load the client.html file in the browser, the server shows the following errors:

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

There are two variables that are not defined, and the function socket_select() returns the error "5 is not a valid Socket resource"

In the browser I get the "Disconnected" message as soon as the file is loaded.

I tried to make the server work locally using XAMPP (Apache and PHP) but I got the same errors. I also tried to change ports and follow the instructions in this issue:

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

But I still get the error "5 is not a valid Socket resource"

I remember that refreshing the page several times I got it running a few months ago, but now it's impossible. Besides, I need it to work all the time, not just after I refresh the page like 20 times.

I also tried with the websocket.class.php file, but this time I get an error on the client side. The browser now returns "Error during WebSocket handshake: 'Sec-WebSocket-Accept' header is missing".

So, I can't make it work with old or new files, with remote or local server, with magic or a ouija board!

Any idea?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橙幽之幻 2024-12-08 19:09:14

从最新的 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;
}

Starting with the newest phpwebsocket client.html and server.php files I replaced both the getheaders() and dohandshake() functions with the code below works for me in newest Chrome. However it currently does not write to the browser nor stay alive after one uer comment in the chat box.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文