Php 如何在无休止的循环中每3分钟回音一次?
我有一个脚本,它使用Php 如何在无休止的循环中每3分钟回音一次?,php,html,xml,xmpphp,Php,Html,Xml,Xmpphp,我有一个脚本,它使用while(true)来运行,所以它会一直运行到死 我希望能够使它每3分钟发送一次消息,并重新连接每次断开的连接,我如何才能做到这一点 用于的脚本在Jabber服务器上运行,使用PHP托管,因此很混乱,而且我不确定如何使它每3分钟运行一次,并在断开连接时自动重新连接,因为如果我使用sleep()或usleep()脚本将堆叠,脚本自动响应消息将不会运行 那我怎么才能做到呢?有人能帮我吗 try { while(!$this->disconnect()) { $
while(true)
来运行,所以它会一直运行到死
我希望能够使它每3分钟发送一次消息,并重新连接每次断开的连接,我如何才能做到这一点
用于的脚本在Jabber服务器上运行,使用PHP托管,因此很混乱,而且我不确定如何使它每3分钟运行一次,并在断开连接时自动重新连接,因为如果我使用sleep()
或usleep()
脚本将堆叠,脚本自动响应消息将不会运行
那我怎么才能做到呢?有人能帮我吗
try {
while(!$this->disconnect()) {
$this->connect();
while(!$this->isDisconnected()) {
$starts = $this->processUntil(array('message', 'session_start'));
foreach($starts as $go) {
$new = $go[1];
switch($go[0]) {
case 'session_start':
break;
case 'message':
$filter = $show="online";
if($new['from'] == $filter) {
$sender = explode('@', $new['from']);
$this->message($new['from'], $body="Auto Respond Message: Sorry $sender[0] Iam Not Here Right Now", $type="chat");
}
$the_time = time();
$interval = 3*60;
while(true) {
if ($the_time + $interval >= time()) {
$this->message($myself, $body="PING !!!", $type="chat");
$the_time = time();
}
$this->presence($status="ONLINE !!!", $show="online");
}
break;
}
}
}
}
} catch(XMPPHP_Exception $e) {
die($e->getMessage());
}
使用睡眠功能:
你可以使用sleep()
试着这样做:
<?php
while (@ob_end_flush());
try {
while (!$this->disconnect()) {
$this->connect();
while (!$this->isDisconnected()) {
$starts = $this->processUntil(array('message', 'session_start'));
foreach ($starts as $go) {
$new = $go[1];
switch ($go[0]) {
case 'session_start':
break;
case 'message':
$filter = $show = "online";
if ($new['from'] == $filter) {
$sender = explode('@', $new['from']);
$this->message($new['from'], $body = "Auto Respond Message: Sorry $sender[0] Iam Not Here Right Now", $type = "chat");
}
$the_time = time();
$interval = 3 * 60;
while (true) {
if ($the_time + $interval >= time()) {
$this->message($myself, $body = "PING !!!", $type = "chat");
ob_flush();
flush();
$the_time = time();
}
$this->presence($status = "ONLINE !!!", $show = "online");
}
break;
}
}
}
}
} catch (XMPPHP_Exception $e) {
die($e->getMessage());
}
使用循环并在睡眠中回显文本
// sleep for 20 seconds
while( true )
{
echo "text here!";
sleep(20);
}
它将在1分钟内回显文本3次。如果我使用sleep(180)或usleep(180000000),则自动回复消息脚本将不会运行。顺便说一句,我已经试过了。你确定你的剧本没有因为超时而被杀吗?是的,当然,兄弟。如果脚本可以每3分钟发送PING消息。因为基本上是自动回复信息的脚本。我想我得到了你想要的。。我认为你应该寻找另一种方法。一旦收到混乱/老化,您必须调用此函数,而不是保持此脚本处于唤醒状态(使用sleep或其他方法)。我是对的,仍然没有让脚本每3分钟发送PING消息,兄弟,但是谢谢。OP特别说他不能使用睡眠
<?php
while (@ob_end_flush());
try {
while (!$this->disconnect()) {
$this->connect();
while (!$this->isDisconnected()) {
$starts = $this->processUntil(array('message', 'session_start'));
foreach ($starts as $go) {
$new = $go[1];
switch ($go[0]) {
case 'session_start':
break;
case 'message':
$filter = $show = "online";
if ($new['from'] == $filter) {
$sender = explode('@', $new['from']);
$this->message($new['from'], $body = "Auto Respond Message: Sorry $sender[0] Iam Not Here Right Now", $type = "chat");
}
$the_time = time();
$interval = 3 * 60;
while (true) {
if ($the_time + $interval >= time()) {
$this->message($myself, $body = "PING !!!", $type = "chat");
ob_flush();
flush();
$the_time = time();
}
$this->presence($status = "ONLINE !!!", $show = "online");
}
break;
}
}
}
}
} catch (XMPPHP_Exception $e) {
die($e->getMessage());
}
// sleep for 20 seconds
while( true )
{
echo "text here!";
sleep(20);
}