1

我有一个while(true)用来运行的脚本,所以它会永远运行直到它死掉。

我希望能够让它每 3 分钟发送一次消息并在每次断开连接时重新连接,我该怎么做?

该脚本在使用 PHP 托管的 Jabber 服务器上运行,因此令人困惑,而且我不知道如何让它每 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());
}
4

4 回答 4

2

使用睡眠功能: http: //php.net/manual/en/function.sleep.php

// sleep for 30 seconds
sleep(30);
于 2015-01-23T12:26:59.130 回答
0

使用循环并在睡眠中回显您的文本;

// sleep for 20 seconds
while( true )
{
   echo "text here!";
   sleep(20);
}

它将在 1 分钟内回显文本 3 次。

于 2015-01-23T12:36:49.120 回答
0

你可以使用睡眠();

echo $statement1;
sleep(180);
echo $statement2;
于 2015-01-23T12:28:07.520 回答
0

尝试这样的事情:

<?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());
} 
于 2015-01-23T12:30:37.133 回答