问题描述
我正在开发一个网页,该网页使用PHP来获取位于同一台计算机上的某些服务器的状态(正好是7台服务器)。
当我的时候只获得1台服务器的状态,页面确实加载速度快,一切正常,但是在添加所有服务器之后需要加载5秒钟,有时它会超时并且根本不加载。
要获取服务器状态,我使用的是.php,它使用套接字:
I'm working on a webpage which uses PHP to get the state of some servers located in the same machine (exactly 7 servers).
When I was only getting the state of 1 server the page did load fast and everything was ok, but after adding all the servers it takes like 5 seconds to load and sometimes it times out and doesn't load at all.
To get the server status I am using this .php which uses sockets:
<html>
<?php
class Status {
public $server;
public $online, $motd, $online_players, $max_players;
public $error = "OK";
function __construct($url, $port = '25565') {
$this->server = array(
"url" => $url,
"port" => $port
);
if ( $sock = @stream_socket_client('tcp://'.$url.':'.$port, $errno, $errstr, 1) ) {
$this->online = true;
fwrite($sock, "\xfe");
$h = fread($sock, 2048);
$h = str_replace("\x00", '', $h);
$h = substr($h, 2);
$data = explode("\xa7", $h);
unset($h);
fclose($sock);
if (sizeof($data) == 3) {
$this->motd = $data[0];
$this->online_players = (int) $data[1];
$this->max_players = (int) $data[2];
}
else {
$this->error = "Cannot retrieve server info.";
}
}
else {
$this->online = false;
$this->error = "Cannot connect to server.";
}
}
}
?>
</html>
在服务器状态显示php代码的页面上是这样的:
And on the page where the state of the servers display the php code is like this:
<?php
$server = new Status("127.0.0.1", 54122);
echo "(";
echo $server->online_players;
echo "/";
echo $server->max_players;
echo ")";
$var = $server->online;
if ($var == true) {
echo "<a href='#' class='online'>ONLINE</a>";
}
else {
echo "<a href='#' class='offline'>OFFLINE</a>";
}
?>
服务器的状态检索好了,一切正常,但我是什么想知道是否有人有任何想法避免网页超时或以任何方式改善加载时间?
谢谢。
The status of the server is retrieved okay and everything works, but what I wanted to know if anyone has any idea to avoid the webpage timing out or improve the load time in any way?
Thanks.
推荐答案
这篇关于PHP网页大加载时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!