12

编辑: 我试过这个 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 localhost 上的 Chrome 17.0.963.56 中尝试它,并启用了 php_sockets 扩展。

我在某处看到有人提到 Chrome 更改了它支持的内容,但没有深入探讨如何修复它。我希望有人能帮我完成它。(我是 websockets 的新手)。

服务器:

{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:请求握手……</p>

2012-02-20 07:03:01 WebSocket:握手……</p>

2012-02-20 07:03:01 WebSocket:完成握手......</p>

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

客户:

套接字状态:0

插座状态:3(关闭)

4

3 回答 3

1

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

实际上,我只是下载并测试了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

服务器发回的标头是:

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”。

新问题:即使网络套接字连接现在在握手时工作,它似乎也在发送第一条消息时断开连接。

于 2012-03-17T16:37:33.217 回答
0

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

于 2012-06-30T04:13:07.537 回答
-1

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

    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 是Sec-WebSocket-Key从 $buffer 得到的,你可以 print_r($buffer) 看看。

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

于 2013-03-06T17:53:45.153 回答