1

I have a script that uses while(true) to run so it runs forever until it dies.

I want to be able to make it send a message once every 3 minutes and reconnecting every disconnect, how can I do this?

The script for runs on a Jabber server with using PHP hosting, so it's confusing, plus I am not sure how to make it do that every 3 minutes and automatic reconnecting while disconnect, because if I am using sleep() or usleep() the script will stack and script auto respond message will not run.

So how can I make it? Can somebody help me?

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());
}
Biffen
  • 6,249
  • 6
  • 28
  • 36

4 Answers4

2

Use the sleep function: http://php.net/manual/en/function.sleep.php

// sleep for 30 seconds
sleep(30);
Sergio Costa
  • 610
  • 6
  • 14
  • if i am using sleep(180) or usleep(180000000) the autorespond message script will not run. i already try that btw. – kid.silencer Jan 23 '15 at 12:36
  • Are you sure your script is not getting killed due to it timing out? – Jono20201 Jan 23 '15 at 12:43
  • yes sure brother. If the script can send PING message every 3 minutes. Because basicly that script for auto respond message. – kid.silencer Jan 23 '15 at 12:51
  • I think that I got it what you are looking for.. I believe that you should look for another approach. You have to call this function once you receive a mess/age, not keep this script awake (using sleep ou something else). I am right – Sergio Costa Jan 23 '15 at 14:34
0

you can use sleep();

echo $statement1;
sleep(180);
echo $statement2;
Andy
  • 49,085
  • 60
  • 166
  • 233
Cruzer
  • 143
  • 1
  • 10
0

Try something like this:

<?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());
} 
Rahul Shandilya
  • 126
  • 1
  • 6
0

Use loop and echo your text with sleep;

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

It will echo text 3 times in 1 minute.

Huzoor Bux
  • 1,026
  • 4
  • 20
  • 46