gpt4 book ai didi

php - 套接字在PHP中这么慢吗?

转载 作者:行者123 更新时间:2023-12-03 11:56:23 24 4
gpt4 key购买 nike

我正在将此代码用于套接字。控制台表示页面处理时间非常短,但是Chrome表示页面加载时间约为1秒钟!

    $this->serv_sock = socket_create(AF_INET, SOCK_STREAM, 0); 

socket_bind($this->serv_sock, $this->serv, $this->port) or die("Could not bind to address\n");

socket_listen($this->serv_sock);

while (1) {
echo "Waiting...\n";
$client = socket_accept($this->serv_sock);

$start_mtime = microtime(true);

echo "Accepted at ".$start_mtime.".\n";

$input = '';

$len = 0;

do {
//echo "Reading.\n";
$inp = socket_read($client, 1024);
$input .= $inp;

if (strpos($input, "\n\n") === false && strpos($input, "\r\n\r\n") === false)
continue;

if (!$len) {
if (!preg_match("/Content-Length: (\d+)/", $input, $matches)) {
break;
}
$len = $matches[1];
if (!$len)
break;
echo "We want $len bytes.\n";
}

if (strpos($input, "\n\n") !== false)
list($headers, $content) = explode("\n\n", $input);
else
list($headers, $content) = explode("\r\n\r\n", $input);

if (strlen($content) >= $len)
break;
} while ($inp);

echo "Calling callback as ".microtime(true).".\n";

if (strpos($input, "\n\n") !== false)
list($headers, $content) = explode("\n\n", $input);
else
list($headers, $content) = explode("\r\n\r\n", $input);

$output = $this->translate($callback, $headers, $content); // nothing slow here

$time_end = microtime(true);
echo "Sending output at ".$time_end." (total ".($time_end - $start_mtime).")\n\n";

$output = "HTTP/1.0 Ok\n".
"Content-Type: text/html; charset=utf-8\n".
"Content-Length: ".strlen($output)."\n".
"Connection: close\n\n".
$output;

socket_write($client, $output);

socket_close($client);
}

最佳答案

如果我明白您的意思。据我所知,服务器处理时间和客户端处理时间之间存在差异。

在服务器上处理信息的实际时间总是会更少。服务器完成信息处理后,仍然必须将数据发送到浏览器并进行渲染。我怀疑数据到达那里和浏览器呈现的时间是因为Chrome告诉您它需要大约1秒钟的时间。

关于php - 套接字在PHP中这么慢吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1842680/

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