gpt4 book ai didi

php - 如何修复 Websocket 握手代码?

转载 作者:行者123 更新时间:2023-12-04 20:22:59 25 4
gpt4 key购买 nike

这可能是一个耳熟能详的悲惨故事。但是那里有太多这样的问题,而我是一个菜鸟,我找不到答案,所以如果你能帮助我,我希望得到你的帮助。

因此,我正在使用 lemmingzshadow 的 phpwebsocket(如果您不熟悉,谷歌会很容易地提出这个问题)。据我所知,他发布的版本有一个错误,它不符合 Chrome 20.+ 现在使用的标准。它与握手和安全 key 有关,但这就是我坚持的地方。我知道我需要根据其他问题提供以下信息,希望您能帮助我理解和解决这个问题:

Chrome 收到的 header 是(已编辑;我显然将消息发布到服务器两次。):

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: aWXrpLOnEm15mE8+w1zG05ad01k=
Sec-WebSocket-Protocol: QuatroDuo

我的服务器收到的 header 是:

Upgrade: websocket
Connection: Upgrade
Host: gumonshoe.net:8000
Origin: http://gumonshoe.net
Sec-WebSocket-Key: v3+iw0U78qkwZnp+RWTu3A
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame

我认为 cookie 不是必需的,如果我错了请纠正我。

我不想做下一部分,但我认为全部粘贴总比什么都不做要稍后再回来要好。这是读取和解释握手并发送新握手的代码部分。

感谢帮助:

<?PHP
private function handshake($data)
{
$this->log('Performing handshake\r\n\r\n' . $data);
$lines = preg_split("/\r\n/", $data);

// check for valid http-header:
if(!preg_match('/\AGET (\S+) HTTP\/1.1\z/', $lines[0], $matches)) {
$this->log('Invalid request: ' . $lines[0]);
$this->sendHttpResponse(400);
stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
return false;
}

// check for valid application:
$path = $matches[1];
$this->application = $this->server->getApplication(substr($path, 1));
if(!$this->application) {
$this->log('Invalid application: ' . $path);
$this->sendHttpResponse(404);
stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
$this->server->removeClientOnError($this);
return false;
}

// generate headers array:
$headers = array();
foreach($lines as $line)
{
$line = chop($line);
if(preg_match('/\A(\S+): (.*)\z/', $line, $matches))
{
$headers[$matches[1]] = $matches[2];
}
}

// check for supported websocket version:
if(!isset($headers['Sec-WebSocket-Version']) || $headers['Sec-WebSocket-Version'] < 6)
{
$this->log('Unsupported websocket version.');
$this->sendHttpResponse(501);
stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
$this->server->removeClientOnError($this);
return false;
}

// check origin:
if($this->server->getCheckOrigin() === true)
{
$origin = (isset($headers['Sec-WebSocket-Origin'])) ? $headers['Sec-WebSocket-Origin'] : false;
$origin = (isset($headers['Origin'])) ? $headers['Origin'] : $origin;
if($origin === false)
{
$this->log('No origin provided.');
$this->sendHttpResponse(401);
stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
$this->server->removeClientOnError($this);
return false;
}

if(empty($origin))
{
$this->log('Empty origin provided.');
$this->sendHttpResponse(401);
stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
$this->server->removeClientOnError($this);
return false;
}

if($this->server->checkOrigin($origin) === false)
{
$this->log('Invalid origin provided. : ' . $origin . ' Legal options were:');
$gumk = 0;
foreach(array_keys($this->server->getAllowedOrigins()) as $lo) {
$this->log( '[' . $gumk++ . '] : ' . $lo);
}
$this->sendHttpResponse(401);
stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
$this->server->removeClientOnError($this);
return false;
}
}

// do handyshake: (hybi-10)
$secKey = $headers['Sec-WebSocket-Key'];
$secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
$response = "HTTP/1.1 101 Switching Protocols\r\n";
$response.= "Upgrade: websocket\r\n";
$response.= "Connection: Upgrade\r\n";
$response.= "Sec-WebSocket-Accept: " . $secAccept . "\r\n";
$response.= "Sec-WebSocket-Protocol: " . substr($path, 1) . "\r\n\r\n";
if(false === ($this->server->writeBuffer($this->socket, $response)))
{
return false;
}
$this->handshaked = true;
$this->log('Handshake sent');
$this->application->onConnect($this);

// trigger status application:
if($this->server->getApplication('status') !== false)
{
$this->server->getApplication('status')->clientConnected($this->ip, $this->port);
}

return true;
}

收到以下错误,

Error during WebSocket handshake: Sec-WebSocket-Protocol mismatch

由于我在这种级别的服务器调试方面基本上没有经验,所以如果能提供比将我链接到文档/规范更详细的答案,我们将不胜感激。

最佳答案

如果你们中有人用头撞墙,这就是有问题的代码:

$response.= "Sec-WebSocket-Protocol: " . substr($path, 1) .

我确信有一种方法可以实际设置所需/可能的协议(protocol),但我不确定它们是什么;而且我不确定它对我的目的是否必要。如果有人解释了协议(protocol)切换的用途,我很乐意阅读它,但现在我只是将其从我的代码中删除。

大量谷歌搜索来发现这个小问题。

我还在握手中丢弃了 pack(H*) 代码,根据我阅读的内容,这似乎不是必需的。我不确定这是否有任何作用,但没有必要让程序运行。

关于php - 如何修复 Websocket 握手代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11406164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com