“Sec-WebSocket-Accept” Chrome 17 中缺少标头

发布于 2025-01-07 05:03:27 字数 1257 浏览 0 评论 0原文

编辑: 我尝试了这个 phpwebsocket: http://www.wilky.it/Shared/phpwebsocket.zip 它可以在 Firefox 中使用,但我的问题仍然存在:如何让 websockets 在 Chrome 17 中与 php 服务器一起使用?


我正在关注这里的教程: http:// net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/

看起来好像客户端连接,然后立即断开连接。我在控制台中注意到这个错误:

WebSocket 握手期间出错:“Sec-WebSocket-Accept”标头丢失

我正在 WAMP 本地主机上的 Chrome 17.0.963.56 中尝试,并启用了 php_sockets 扩展。

我在某处看到有人提到 Chrome 改变了它所支持的内容,但没有深入探讨如何修复它。我希望有人能引导我度过难关。 (我是网络套接字的新手)。

服务器:

{PATH}>php startDaemon.php

2012-02-20 07:02:51 系统:已创建套接字资源 ID #7。

2012-02-20 07:02:51 系统:套接字绑定到 localhost:8000。

2012-02-20 07:02:51 系统:开始监听 Socket。

2012-02-20 07:03:01 WebSocket:资源 id #8 已连接!

2012-02-20 07:03:01 WebSocket:请求握手...

2012-02-20 07:03:01 WebSocket:握手...

2012-02-20 07:03:01 WebSocket:握手完成...

2012-02-20 07:03:01 WebSocket:资源 id #8 已断开连接!

客户:

套接字状态:0

套接字状态:3(关闭)

Edit:
I tried this phpwebsocket: http://www.wilky.it/Shared/phpwebsocket.zip and it works in Firefox, but my question still remains: how do I get websockets to work with a php server in Chrome 17?


I'm following the tutorial here: http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/

It appears as though the client connects, and then immediately disconnects. I noticed this error in the console:

Error during WebSocket handshake: 'Sec-WebSocket-Accept' header is missing

I'm trying it in Chrome 17.0.963.56 on my WAMP localhost with the php_sockets extension enabled.

I saw mentioned somewhere that Chrome had changed what it supported, but it didn't go in depth on how to fix it. I was hoping someone could step me through it. (I'm brand new to websockets).

Server:

{PATH}>php startDaemon.php

2012-02-20 07:02:51 System: Socket Resource id #7 created.

2012-02-20 07:02:51 System: Socket bound to localhost:8000.

2012-02-20 07:02:51 System: Start listening on Socket.

2012-02-20 07:03:01 WebSocket: Resource id #8 CONNECTED!

2012-02-20 07:03:01 WebSocket: Requesting handshake…

2012-02-20 07:03:01 WebSocket: Handshaking…

2012-02-20 07:03:01 WebSocket: Done handshaking…

2012-02-20 07:03:01 WebSocket: Resource id #8 disconnected!

Client:

Socket Status: 0

Socket Status: 3 (Closed)

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

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

发布评论

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

评论(3

猫七 2025-01-14 05:03:27

我也有同样的问题(而且我似乎无法在这里发表评论,所以我发表了回复)。

实际上,我刚刚下载并测试了 phpwebsocket。

在 safari 5.1.4 上,它工作得很好。

在 Chrome 17 上,我在脚本日志控制台中遇到了相同的错误:

Error during WebSocket handshake: 'Sec-WebSocket-Accept' header is missing

因此,在 websocket.class.php 中,我添加到服务器返回的标头:

$accept = base64_encode(SHA1($key."258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));

我收到错误:

Error during WebSocket handshake: Sec-WebSocket-Accept mismatch

现在,服务器收到的标头是:

GET /websocket/server.php HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:12345
Origin: http://localhost:8888
Sec-WebSocket-Key: OqMJI0t/cOl6d6JNE+Op0g==
Sec-WebSocket-Version: 13

并且服务器发回的 header 是:

HTTP/1.1 101 WebSocket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Origin: http://localhost:8888
Sec-WebSocket-Location: ws://localhost:12345/websocket/server.php
Sec-WebSocket-Accept: ZjY5ODliNTViYzJlOTNkMjk4OTg3Y2U2NjQ3MTBlZjZiNzliYzk4Yg==

Sec-WebSocket-Accept 看起来不错,但仍然存在不匹配错误。您是否发现某个地方有错误?也许协议已更改以计算 Sec-WebSocket-Accept,但我没有找到它......感谢您的帮助!

编辑:这似乎是解决方案(至少对我来说):将参数 true 添加到 SHA1 函数,如 此问题线程。因此,必须像这样找到 Sec-WebSocket-Accept:

$accept = base64_encode(SHA1($key."258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));

并且,Sec-WebSocket-Key1 和 Sec-WebSocket-Key2 似乎不再出现在客户端请求中,相反,必须从标头中提取 $key: “Sec-WebSocket-Key”。

新问题:即使 Web 套接字连接现在可以在握手时正常工作,但在发送第一条消息时它也会断开连接。

I have the same problem (and I do not seem to be able to post a comment here, so I post a reply).

Actually, I just downloaded and tested phpwebsocket.

On safari 5.1.4, it works just fine.

On Chrome 17, I got the same error in the script log console:

Error during WebSocket handshake: 'Sec-WebSocket-Accept' header is missing

So, in websocket.class.php, I added to the header returned by server:

$accept = base64_encode(SHA1($key."258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));

And I get the error:

Error during WebSocket handshake: Sec-WebSocket-Accept mismatch

Now, the header received by the server is:

GET /websocket/server.php HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:12345
Origin: http://localhost:8888
Sec-WebSocket-Key: OqMJI0t/cOl6d6JNE+Op0g==
Sec-WebSocket-Version: 13

And the header sent back by the server is:

HTTP/1.1 101 WebSocket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Origin: http://localhost:8888
Sec-WebSocket-Location: ws://localhost:12345/websocket/server.php
Sec-WebSocket-Accept: ZjY5ODliNTViYzJlOTNkMjk4OTg3Y2U2NjQ3MTBlZjZiNzliYzk4Yg==

The Sec-WebSocket-Accept seems good, but still there is a mismatch error. Do you see a mistake somewhere? Maybe the protocol has changed to calculate the Sec-WebSocket-Accept, but I don't find it... Thanks for your help!

Edit: Here seems to be the solution (for me, at least): adding the parameter true to the SHA1 function, as found in files given in this issue thread. So, the Sec-WebSocket-Accept must be found like this:

$accept = base64_encode(SHA1($key."258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));

And, Sec-WebSocket-Key1 and Sec-WebSocket-Key2 does not seem to be present anymore in the client request, instead, $key must be extracted from the header: "Sec-WebSocket-Key".

New issue: It seems too that even if the web socket connection now works on the handshake, it disconnects when the first message is sent.

那支青花 2025-01-14 05:03:27

我注意到在 Chrome 19 的控制台中:
服务器不得屏蔽发送给客户端的任何帧。
也许这就是问题所在。一旦发送消息,它就会断开连接。它在 Firefox 中运行良好。

我修复了这个 websocket 问题,现在它可以在 Chrome 中运行了。
首先我使用:

然后我使用以下的编码函数:
https://github.com/lemmingzshadow/php-websocket

我修复了替换的编码函数lemmingzshadow 的 github 中的 connection.php 文件中的那个,它开始工作了。该函数在 \server\lib\WebSocket\connection.php 文件中调用:hybi10Encode。

将此参数在函数编码中更改: $masked = true 至 $masked = false

I noticed that in the console of Chrome 19:
A server must not mask any frames that it sends to the client.
Maybe this is the problem. It disconnects as soon as a message is sent. It works fine in Firefox.

I fixed this websocket problem and it works in chrome now.
First I used:

Then I used the encode function from:
https://github.com/lemmingzshadow/php-websocket

I fixed the replaced the encode function with the one in the connection.php file in lemmingzshadow’s github and it started working. The function is called: hybi10Encode in the \server\lib\WebSocket\connection.php file.

change this parameter in the function encode: $masked = true to $masked = false

百合的盛世恋 2025-01-14 05:03:27

一个简单的修复方法是在 do_handshake 时添加 Sec-WebSocket-Accept 信息,代码如下:

    list($resource,$host,$origin,$key) = $this->getheaders($buffer);

    $accept = base64_encode(SHA1($key."258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));

    $upgrade  = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
            "Upgrade: WebSocket\r\n" .
            "Connection: Upgrade\r\n" .
            "WebSocket-Origin: {$origin}\r\n" .
            "WebSocket-Location: ws://{$host}{$resource}\r\n".
            "Sec-WebSocket-Accept: " . $accept . "\r\n\r\n";
    $this->handshakes[$socket_index] = true;

    socket_write($socket,$upgrade,strlen($upgrade));

其中,

$accept = base64_encode(SHA1($key."258EAFA5-E914-47DA-95CA -C5AB0DC85B11", true));

$key 是从 $buffer 获取的 Sec-WebSocket-Key,你可以 print_r($buffer) 看看。

希望这可以解决您的问题..

An EASY way to fix is add Sec-WebSocket-Accept information when do_handshake, code as below:

    list($resource,$host,$origin,$key) = $this->getheaders($buffer);

    $accept = base64_encode(SHA1($key."258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));

    $upgrade  = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
            "Upgrade: WebSocket\r\n" .
            "Connection: Upgrade\r\n" .
            "WebSocket-Origin: {$origin}\r\n" .
            "WebSocket-Location: ws://{$host}{$resource}\r\n".
            "Sec-WebSocket-Accept: " . $accept . "\r\n\r\n";
    $this->handshakes[$socket_index] = true;

    socket_write($socket,$upgrade,strlen($upgrade));

where,

$accept = base64_encode(SHA1($key."258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));

$key is Sec-WebSocket-Key got from $buffer, you can print_r($buffer) to have a look.

Hope this can solve your problem..

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