有问题实时更新网页元素 [英] Problem updating webpage elements in real time

查看:76
本文介绍了有问题实时更新网页元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开设一个网页,实时显示游戏服务器上的在线玩家数量。



问题是我可以获得玩家在网站上显示的游戏服务器在线,但在加载页面后它永远不会更新,并且它始终保持为初始玩家数量,尽管很多人每秒加入和离开服务器。



这是我用来显示数字的PHP代码:



I am working on a webpage that shows the amount of online players on a game server in real time.

The problem is that I can get the amount of players online in the game server displayed in the website, but after loading the page it never updates and it always stays as the initial amount of players, although lots of people join and leave the server every second.

This is the PHP code that I am using to show the numbers:

<?php
	echo "<a id='a1' href='#' class='online'>Loading...</a>";
?>





我正在做的是每秒使用新的在线玩家数量更新'a1' javascript,用一个名为'getplayers()'的php函数更新它:





What I am doing is to update 'a1' every second with the new amount of online players using javascript, which updates it with a php function called 'getplayers()':

<script language="JavaScript">
setInterval(function(){
    document.getElementById("a1").innerHTML = '<?php echo getplayers()?>';
}, 1000);
</script>







函数'getplayers()'如:






The function 'getplayers()' is like:

<?php
include "Status.php";
function getplayers() {
	$serverb = new Status("127.0.0.1", '17171');
	return $serverb->online_players;
}
?>







最后,Status.php是一个php脚本可以获取在线玩家的数量以及有关服务器的更多信息,我相信这有效:






Lastly, Status.php is a php script that gets the amount of players online and more things about the server, which I am sure that works:

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





所以我的问题是,如果有人知道为什么它总是用第一个数量的球员更新而不是投入新的球员数量?



So my question is if someone knows why it always updates with the first number of players instead of putting the new number of players?

推荐答案

serverb = new Status(127.0.0.1,'17171');
return
serverb = new Status("127.0.0.1", '17171'); return


serverb-> online_players;
}
?>
serverb->online_players; } ?>







最后,Status.php是一个php脚本可以获取在线玩家的数量以及有关服务器的更多信息,我相信这有效:






Lastly, Status.php is a php script that gets the amount of players online and more things about the server, which I am sure that works:

<html>
<?php

class Status {

    public 


server ;
public
server; public


这篇关于有问题实时更新网页元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆