Beispiel #1
0
 public function check()
 {
     if ($this->start === 0) {
         $this->start = time();
     }
     if (SSEUtils::time_mod($this->start, $this->period) == 0) {
         return true;
     } else {
         return false;
     }
 }
Beispiel #2
0
 public function start()
 {
     @set_time_limit(0);
     //disable time limit
     //send the proper header
     header('Content-Type: text/event-stream');
     header('Cache-Control: no-cache');
     if ($this->allow_cors) {
         header('Access-Control-Allow-Origin: *');
         header('Access-Control-Allow-Credentials: true');
     }
     if ($this->use_chunked_encoding) {
         header('Transfer-encoding: chunked');
     }
     //prevent buffering
     if (function_exists('apache_setenv')) {
         @apache_setenv('no-gzip', 1);
     }
     @ini_set('zlib.output_compression', 0);
     @ini_set('implicit_flush', 1);
     while (ob_get_level() != 0) {
         ob_end_flush();
     }
     ob_implicit_flush(1);
     $start = time();
     //record start time
     echo 'retry: ' . $this->client_reconnect * 1000 . "\n";
     //set the retry interval for the client
     //keep the script running
     while (true) {
         if (SSEUtils::time_mod($start, $this->keep_alive_time) == 0) {
             //No updates needed, send a comment to keep the connection alive.
             //From https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events
             echo ': ' . sha1(mt_rand()) . "\n\n";
         }
         //start to check for updates
         foreach ($this->_handlers as $event => $handler) {
             if ($handler->check()) {
                 //check if the data is avaliable
                 $data = $handler->update();
                 //get the data
                 $this->id++;
                 SSEUtils::sseBlock($this->id, $event, $data);
                 //make sure the data has been sent to the client
                 @ob_flush();
                 @flush();
             } else {
                 continue;
             }
         }
         //break if the time excceed the limit
         if ($this->exec_limit != 0 && SSEUtils::time_diff($start) > $this->exec_limit) {
             break;
         }
         //sleep
         usleep($this->sleep_time * 1000000);
     }
 }
Beispiel #3
0
}
*/
while (true) {
    //    usleep($_sleep_time * 1000000);
    //	printf ('data: {"op" : "setOwnerSignature", "time" : "%s"}' . "\n\n", date('d/m H:i:s'));
    //print 'data: {"opid" : "approved"}' . PHP_EOL . PHP_EOL;
    //ob_flush();
    //flush();
    //if (false) {
    if ($ob = check_global()) {
        //error_log($ob . PHP_EOL, 3, "error.log");
        //set_global(0);
        //error_log("Cycle: " . date('d/m H:i:s') . " --- " . session_id() . PHP_EOL, 3, "error.log");
        send($ob);
    } else {
        if (SSEUtils::time_mod($_start, $_keep_alive_time) == 0) {
            //error_log(date('d/m H:i:s'), 3, "error.log");
            //error_log((time() - $_start) . PHP_EOL, 3, "error.log");
            //error_log($_start . PHP_EOL, 3, "error.log");
            //No updates needed, send a comment to keep the connection alive.
            //From https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events
            echo ': ' . sha1(mt_rand()) . "\n\n";
            ob_flush();
            flush();
        }
    }
    usleep($_sleep_time * 1000000);
}
class SSEUtils
{
    /*