我正在创建这个列出我的世界服务器的网站。基本上,我正在尝试在显示所有这些服务器时对它们进行 ping 操作。用 php 解决这个问题对我来说并不能完全解决,ping 所有服务器需要时间,而且我知道 javascript 可以同时执行多个“ping”。我该怎么做?
我现在使用的 PHP 代码:
class minecraft_server
{
private $address;
private $port;
public function __construct($address, $port = 25565){
$this->address = $address;
$this->port = $port;
}
public function get_ping_info(&$info){
$starttime = microtime(true);
$socket = @fsockopen($this->address, $this->port, $errno, $errstr, 1.0);
$stoptime = microtime(true);
$ping = round(($stoptime-$starttime)*1000);
if ($socket === false){
return false;
}
fwrite($socket, "\xfe\x01");
$data = fread($socket, 256);
if (substr($data, 0, 1) != "\xff"){
return false;
}
if (substr($data, 3, 5) == "\x00\xa7\x00\x31\x00"){
$data = explode("\x00", mb_convert_encoding(substr($data, 15), 'UTF-8', 'UCS-2'));
}else{
$data = explode('§', mb_convert_encoding(substr($data, 3), 'UTF-8', 'UCS-2'));
}
if (count($data) == 3){
$info = array(
'version' => '1.3.2',
'motd' => $data[0],
'players' => intval($data[1]),
'max_players' => intval($data[2]),
'ping' => $ping
);
}else{
$info = array(
'version' => $data[0],
'motd' => $data[1],
'players' => intval($data[2]),
'max_players' => intval($data[3]),
'ping' => $ping
);
}
return true;
}
}
你调用这个函数:
$server = new minecraft_server(IP, PORT);
if (!$server->get_ping_info($info)){
echo "Offline";
}else{
print_r($info);
}
我将如何在 javascript 中创建类似的东西?