Hoa central
Public Member Functions | Public Attributes | Protected Attributes | List of all members
Hoa\Eventsource\Server Class Reference

Public Member Functions

 __construct ($verifyHeaders=true)
 
 send ($data, $id=null)
 
 setReconnectionTime ($ms)
 
 __get ($event)
 
 getLastId ()
 

Public Attributes

const MIME_TYPE = 'text/event-stream'
 

Protected Attributes

 $_event = null
 
 $_response = null
 

Detailed Description

Class .

A cross-protocol EventSource server.

Definition at line 49 of file Server.php.

Constructor & Destructor Documentation

Hoa\Eventsource\Server::__construct (   $verifyHeaders = true)

Start an event source.

Parameters
bool$verifyHeadersVerify headers or not.
Returns
void
Exceptions

Definition at line 81 of file Server.php.

82  {
83  if (true === $verifyHeaders && true === headers_sent($file, $line)) {
84  throw new Exception(
85  'Headers already sent in %s at line %d, cannot send data ' .
86  'to client correctly.',
87  0,
88  [$file, $line]
89  );
90  }
91 
92  $mimes = preg_split('#\s*,\s*#', Http\Runtime::getHeader('accept'));
93  $gotcha = false;
94 
95  foreach ($mimes as $mime) {
96  if (0 !== preg_match('#^(\*/\*|' . self::MIME_TYPE . ';?)#', $mime)) {
97  $gotcha = true;
98 
99  break;
100  }
101  }
102 
103  $this->_response = new Http\Response(false);
104 
105  if (false === $gotcha) {
106  $this->_response->sendHeader(
107  'Status',
108  Http\Response::STATUS_NOT_ACCEPTABLE
109  );
110  $this->_response->sendHeader(
111  'Content-Type',
112  'text/plain; charset=utf-8'
113  );
114 
115  throw new Exception(
116  'Client does not accept %s.',
117  1,
118  self::MIME_TYPE
119  );
120  }
121 
122  $this->_response->sendHeader('Content-Type', self::MIME_TYPE);
123  $this->_response->sendHeader('Transfer-Encoding', 'identity');
124  $this->_response->sendHeader('Cache-Control', 'no-cache');
125  $this->_response->sendHeader('X-Accel-Buffering', 'no');
126  $this->_response->newBuffer();
127 
128  return;
129  }
static getHeader($header)
Definition: Runtime.php:181

Here is the call graph for this function:

Member Function Documentation

Hoa\Eventsource\Server::__get (   $event)

Select an event where to send data.

Parameters
string$eventEvent.
Returns
Exceptions

Definition at line 185 of file Server.php.

186  {
187  if (false === (bool) preg_match('##u', $event)) {
188  throw new Exception(
189  'Event name %s must be in UTF-8.',
190  2,
191  $event
192  );
193  }
194 
195  if (0 !== preg_match('#[:' . CRLF . ']#u', $event)) {
196  throw new Exception(
197  'Event name %s contains illegal characters.',
198  3,
199  $event
200  );
201  }
202 
203  $this->_event = $event;
204 
205  return $this;
206  }
Hoa\Eventsource\Server::getLastId ( )

Get last ID.

Returns
string

Definition at line 213 of file Server.php.

214  {
215  return Http\Runtime::getHeader('Last-Event-ID') ?: '';
216  }
static getHeader($header)
Definition: Runtime.php:181

Here is the call graph for this function:

Hoa\Eventsource\Server::send (   $data,
  $id = null 
)

Send an event.

Parameters
string$dataData.
string$idID (empty string to reset).
Returns
void

Definition at line 138 of file Server.php.

139  {
140  if (null !== $this->_event) {
141  $this->_response->writeAll('event: ' . $this->_event . "\n");
142  $this->_event = null;
143  }
144 
145  $this->_response->writeAll(
146  'data: ' .
147  preg_replace("#(" . CRLF . "|\n|\r)#", "\n" . 'data: ', $data)
148  );
149 
150  if (null !== $id) {
151  $this->_response->writeAll("\n" . 'id');
152 
153  if (!empty($id)) {
154  $this->_response->writeAll(': ' . $id);
155  }
156  }
157 
158  $this->_response->writeAll("\n\n");
159  $this->_response->flush(true);
160 
161  return;
162  }
Hoa\Eventsource\Server::setReconnectionTime (   $ms)

Set the reconnection time for the client.

Parameters
int$msTime in milliseconds.
Returns
void

Definition at line 170 of file Server.php.

171  {
172  $this->_response->writeAll('retry: ' . $ms . "\n\n");
173  $this->_response->flush(true);
174 
175  return;
176  }

Member Data Documentation

Hoa\Eventsource\Server::$_event = null
protected

Definition at line 63 of file Server.php.

Hoa\Eventsource\Server::$_response = null
protected

Definition at line 70 of file Server.php.

const Hoa\Eventsource\Server::MIME_TYPE = 'text/event-stream'

Mime type.

string

Definition at line 56 of file Server.php.


The documentation for this class was generated from the following file: