gpt4 book ai didi

php - 使用 php 任何 jquery 或 WebSocket 或 node.js 的中央控制计时器

转载 作者:搜寻专家 更新时间:2023-10-31 21:08:48 27 4
gpt4 key购买 nike

我想用 php 和任何其他客户端脚本设计中央控制计时器。

enter image description here

基本上需要记住的事情是:计时器将在我的网站上可见,因此目前可能有数百名用户登录到我的网站。现在,每当管理员在同一点重置计时器时,它应该反射(reflect)到所有客户端机器上。所以最基本的是需要保持所有机器的同步计时。

我做了什么

我使用了客户端倒数计时器 ( http://keith-wood.name/countdown.html )在后台我每秒调用ajax来检查重置是否被按下。但问题是它没有在所有客户端机器上保持同步。有时两台机器之间的时间差距被标记。那么如何实现呢?

代码:

$('#shortly').countdown({until: shortly,  
onExpiry: liftOff, onTick: watchCountdown});

$('#shortlyStart').click(function() {
shortly = new Date();
shortly.setSeconds(shortly.getSeconds() + 5.5);
$('#shortly').countdown('option', {until: shortly});
});

function liftOff() {
alert('We have lift off!');
}

function watchCountdown(periods) {
$('#monitor').text('Just ' + periods[5] + ' minutes and ' +
periods[6] + ' seconds to go');
}

最佳答案

timerset.php。此文件为服务器端创建文件以推送完成时间。

<?php
$year=htmlspecialchars($_GET['year']);
$month=htmlspecialchars($_GET['month']);
$day=htmlspecialchars($_GET['day']);
$hour=htmlspecialchars($_GET['hour']+2);//NBNBNB the +2 changes according to YOUR timezone.
$minute=htmlspecialchars($_GET['minute']);
$second=htmlspecialchars($_GET['second']);

$timestring=$year.":".$month.":".$day.":".$hour.":".$minute.":".$second;
echo $timestring."<br/>";
$filename = 'timerserver.php';
$file = fopen($filename, 'w');
rewind($file);
fwrite($file,"<?php\n");
fwrite($file, "header('Content-Type: text/event-stream');\n");
fwrite($file, "header('Cache-Control: no-cache');\n");
fwrite($file, "header('connection:keep-alive');\n");
fwrite($file, "\$starttime=\"$timestring\";\n");
fwrite($file, "echo \"data:{\$starttime}\\n\\n\";\n");
fwrite($file, "ob_flush();");
fwrite($file,"flush();");
fwrite($file,"sleep(3);");
fwrite($file,"?>\n");
fflush($file);
ftruncate($file, ftell($file));
fclose($file);
flush();
?>

定时器.php。该文件接收完成时间并更新显示时间。

<html>
<header>
</header>
<body>
<script>

var source = new EventSource("timerserver.php");
var mystarttime="00:00";

source.onmessage = function(event)
{
mystarttime=event.data;
};

setInterval(function () {test(mystarttime)}, 1000);

function test(intime)
{
var timestring=intime;
var split = timestring.split(':');

var currentdate=new Date();
var finishtime=new Date(split[0],split[1]-1,split[2],split[3],split[4],split[5],0);

var timediff=new Date(finishtime-currentdate);

var year=timediff.getUTCFullYear();
var minutes=timediff.getUTCMinutes();
var seconds=timediff.getUTCSeconds();
var currentMinutes = ("0" + minutes).slice(-2);
var currentSeconds = ("0" + seconds).slice(-2);
if (year<1970)
{
document.getElementById("result").innerHTML="Time is up"
}
else
document.getElementById("result").innerHTML = currentMinutes+":"+currentSeconds;
}
</script>
<div id="result">Fetching time...</div>
</body>
</html>

timerform.php 。一种更新时间的方法。

<?php
echo "<b>NB : Time is in UTC Timezone</b><br/><hr/>";
date_default_timezone_set("UTC");
$currentdatetime=date("Y-m-d H:i:s");
$year=date("Y");
$month=date("m");
$day=date("j");
$hour=date("H");
$minute=date("i");
$second=0;
echo $currentdatetime."<hr/>";
?>
<form action="timerreset.php" method="GET">
Year <input name="year" value="<?php echo $year;?>"/><br/>
Month <input name="month" value="<?php echo $month;?>"/><br/>
Day <input name="day" value="<?php echo $day;?>"/><br/><br/>
Hour <input name="hour" value="<?php echo $hour;?>"/><br/>
Minute <input name="minute" value="<?php echo $minute+1;?>"/><br/>
Second <input name="second" value="<?php echo $second;?>"/><br/>
<input type="submit"/>
</form>
<?php
flush();
?>

注意,使用 UTC 时间以满足不同时区的不同客户。

我的本​​地主机 (Windows) 和 SouthCoastHosting (Linux) 都有上面的代码,所以如果您有任何问题,请告诉我。

要使用它,请打开一个选项卡到 southcoasthosting.com/timer/timerform.php 和另一个选项卡到 southcoasthosting.com/timer/timer.php。使用计时器表单更改完成时间,您将在 timer.php 中看到时间变化。由于 EventSource 的性质,在更新计时器之前总是最多有 3 秒的延迟。根据您的客户端与服务器的连接速度,可能会有进一步的延迟。我选择发送完成时间,这样客户就不会因为这些延迟而滞后。

我希望一切都有意义。否则请告诉我。

关于php - 使用 php 任何 jquery 或 WebSocket 或 node.js 的中央控制计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26649449/

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