Example #1
0
 /**
  * Start an event source.
  *
  * @param   bool  $verifyHeaders    Verify headers or not.
  * @return  void
  * @throws  \Hoa\Eventsource\Exception
  */
 public function __construct($verifyHeaders = true)
 {
     if (true === $verifyHeaders && true === headers_sent($file, $line)) {
         throw new Exception('Headers already sent in %s at line %d, cannot send data ' . 'to client correctly.', 0, [$file, $line]);
     }
     $mimes = preg_split('#\\s*,\\s*#', Http\Runtime::getHeader('accept'));
     $gotcha = false;
     foreach ($mimes as $mime) {
         if (0 !== preg_match('#^(\\*/\\*|' . self::MIME_TYPE . ';?)#', $mime)) {
             $gotcha = true;
             break;
         }
     }
     $this->_response = new Http\Response(false);
     if (false === $gotcha) {
         $this->_response->sendHeader('Status', Http\Response::STATUS_NOT_ACCEPTABLE);
         $this->_response->sendHeader('Content-Type', 'text/plain; charset=utf-8');
         throw new Exception('Client does not accept %s.', 1, self::MIME_TYPE);
     }
     $this->_response->sendHeader('Content-Type', self::MIME_TYPE);
     $this->_response->sendHeader('Transfer-Encoding', 'identity');
     $this->_response->sendHeader('Cache-Control', 'no-cache');
     $this->_response->sendHeader('X-Accel-Buffering', 'no');
     $this->_response->newBuffer();
     return;
 }