Hoa central
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Hoa\Websocket\Client Class Reference
Inheritance diagram for Hoa\Websocket\Client:

Public Member Functions

 __construct (Socket\Client $client, $endPoint= '/', Http\Response $response=null)
 
 connect ()
 
 run ()
 
 receive ()
 
 close ($code=self::CLOSE_NORMAL, $reason=null)
 
 getEndPoint ()
 
 setResponse (Http\Response $response)
 
 getResponse ()
 
 setHost ($host)
 
 getHost ()
 
- Public Member Functions inherited from Hoa\Websocket\Connection
 __construct (Socket\Connection $connection)
 
 on ($listenerId, $callable)
 
 send ($message, Socket\Node $node=null, $opcode=self::OPCODE_TEXT_FRAME, $end=true)
 
 close ($code=self::CLOSE_NORMAL, $reason=null)
 
- Public Member Functions inherited from Hoa\Socket\Connection\Handler
 __construct (Connection $connection)
 
 getConnection ()
 
 run ()
 
 merge (self $other)
 
 send ($message, Socket\Node $node=null)
 
 broadcast ($message)
 
 broadcastIf (\Closure $predicate, $message)
 

Protected Member Functions

 doHandshake ()
 
 setEndPoint ($endPoint)
 
- Protected Member Functions inherited from Hoa\Websocket\Connection
 _run (Socket\Node $node)
 
 doHandshake ()
 
 _send ($message, Socket\Node $node)
 
- Protected Member Functions inherited from Hoa\Socket\Connection\Handler
 setConnection (Connection $connection)
 
 getOriginalConnection ()
 
 _run (Socket\Node $node)
 
 _send ($message, Socket\Node $node)
 

Protected Attributes

 $_endPoint = null
 
 $_host = null
 
 $_response = null
 
- Protected Attributes inherited from Hoa\Websocket\Connection
 $_on = null
 
- Protected Attributes inherited from Hoa\Socket\Connection\Handler
 $_originalConnection = null
 
 $_connection = null
 
 $_connections = []
 

Additional Inherited Members

- Public Attributes inherited from Hoa\Websocket\Connection
const OPCODE_CONTINUATION_FRAME = 0x0
 
const OPCODE_TEXT_FRAME = 0x1
 
const OPCODE_BINARY_FRAME = 0x2
 
const OPCODE_CONNECTION_CLOSE = 0x8
 
const OPCODE_PING = 0x9
 
const OPCODE_PONG = 0xa
 
const CLOSE_NORMAL = 1000
 
const CLOSE_GOING_AWAY = 1001
 
const CLOSE_PROTOCOL_ERROR = 1002
 
const CLOSE_DATA_ERROR = 1003
 
const CLOSE_STATUS_ERROR = 1005
 
const CLOSE_ABNORMAL = 1006
 
const CLOSE_MESSAGE_ERROR = 1007
 
const CLOSE_POLICY_ERROR = 1008
 
const CLOSE_MESSAGE_TOO_BIG = 1009
 
const CLOSE_EXTENSION_MISSING = 1010
 
const CLOSE_SERVER_ERROR = 1011
 
const CLOSE_TLS = 1015
 

Detailed Description

Class .

A cross-protocol Websocket client.

Definition at line 51 of file Client.php.

Constructor & Destructor Documentation

Hoa\Websocket\Client::__construct ( Socket\Client  $client,
  $endPoint = '/',
Http\Response  $response = null 
)

Create a Websocket client.

Parameters
\Hoa\Socket\Client$clientClient.
string$endPointEnd-point.
\Hoa\Http\Response$requestResponse parser.
Returns
void
Exceptions

Definition at line 85 of file Client.php.

89  {
90  parent::__construct($client);
91  $this->setEndPoint($endPoint);
92 
93  if (null === $response) {
94  $response = new Http\Response(false);
95  }
96 
97  $this->setResponse($response);
98 
99  return;
100  }
setEndPoint($endPoint)
Definition: Client.php:247
setResponse(Http\Response $response)
Definition: Client.php:271

Here is the call graph for this function:

Member Function Documentation

Hoa\Websocket\Client::close (   $code = self::CLOSE_NORMAL,
  $reason = null 
)

Close a specific node/connection.

Parameters
int$codeCode (please, see self::CLOSE_* constants).
string$reasonReason.
Returns
void

Definition at line 225 of file Client.php.

226  {
227  $connection = $this->getConnection();
228  $protocol = $connection->getCurrentNode()->getProtocolImplementation();
229 
230  if (null !== $protocol) {
231  $protocol->close($code, $reason, true);
232  }
233 
234  $connection->mute();
235  $connection->setStreamTimeout(0, 2 * 15000); // 2 * MLS (on FreeBSD)
236  $connection->read(1);
237 
238  return $connection->disconnect();
239  }

Here is the call graph for this function:

Hoa\Websocket\Client::connect ( )

Connect, i.e. open the connection and do handshake.

Returns
void

Definition at line 107 of file Client.php.

108  {
109  return $this->doHandshake();
110  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Websocket\Client::doHandshake ( )
protected

Try the handshake by trying different protocol implementation.

Returns
void
Exceptions

Definition at line 149 of file Client.php.

150  {
151  static $_tail = ['A', 'Q', 'g', 'w'];
152 
153  $connection = $this->getConnection();
154  $connection->connect();
155  $connection->setStreamBlocking(true);
156 
157  $key =
158  substr(base64_encode(Core::uuid()), 0, 21) .
159  $_tail[mt_rand(0, 3)] . '==';
160 
161  $expected = base64_encode(sha1($key . Protocol\Rfc6455::GUID, true));
162 
163  if (null === $host = $this->getHost()) {
164  throw new Exception(
165  'Host name is null. Please, use the %s::setHost() method.',
166  0,
167  __CLASS__
168  );
169  }
170 
171  $connection->writeAll(
172  $request =
173  'GET ' . $this->getEndPoint() . ' HTTP/1.1' . CRLF .
174  'Host: ' . $host . CRLF .
175  'User-Agent: Hoa' . CRLF .
176  'Upgrade: WebSocket' . CRLF .
177  'Connection: Upgrade' . CRLF .
178  'Pragma: no-cache' . CRLF .
179  'Cache-Control: no-cache' . CRLF .
180  'Sec-WebSocket-Key: ' . $key . CRLF .
181  'Sec-WebSocket-Version: 13' . CRLF . CRLF
182  );
183 
184  $buffer = $connection->read(2048);
185  $response = $this->getResponse();
186  $response->parse($buffer);
187 
188  if ($response::STATUS_SWITCHING_PROTOCOLS !== $response['status'] ||
189  'websocket' !== strtolower($response['upgrade']) ||
190  'upgrade' !== strtolower($response['connection']) ||
191  $expected !== $response['sec-websocket-accept']) {
192  throw new Exception\BadProtocol(
193  'Handshake has failed, the server did not return a valid ' .
194  'response.' . "\n\n" .
195  'Client:' . "\n" . ' %s' . "\n" .
196  'Server:' . "\n" . ' %s',
197  0,
198  [
199  str_replace("\n", "\n" . ' ', $request),
200  str_replace("\n", "\n" . ' ', $buffer)
201  ]
202  );
203  }
204 
205  $currentNode = $connection->getCurrentNode();
206  $currentNode->setHandshake(SUCCEED);
207  $currentNode->setProtocolImplementation(new Protocol\Rfc6455($connection));
208 
209  $this->_on->fire(
210  'open',
211  new Core\Event\Bucket()
212  );
213 
214  return;
215  }
static uuid()
Definition: Core.php:444

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Websocket\Client::getEndPoint ( )

Get end-point.

Returns
string

Definition at line 260 of file Client.php.

261  {
262  return $this->_endPoint;
263  }

Here is the caller graph for this function:

Hoa\Websocket\Client::getHost ( )

Get host.

Returns
string

Definition at line 308 of file Client.php.

309  {
310  return
311  null !== $this->_host
312  ? $this->_host
313  : (isset($_SERVER['HTTP_HOST'])
314  ? $_SERVER['HTTP_HOST']
315  : null);
316  }

Here is the caller graph for this function:

Hoa\Websocket\Client::getResponse ( )

Get response.

Returns

Definition at line 284 of file Client.php.

285  {
286  return $this->_response;
287  }

Here is the caller graph for this function:

Hoa\Websocket\Client::receive ( )

Receive data. Fire listeners.

Returns
void

Definition at line 129 of file Client.php.

130  {
131  $connection = $this->getConnection();
132  $node = $connection->getCurrentNode();
133 
134  do {
135  $this->_run($node);
136  } while (
137  false === $connection->isDisconnected() &&
138  true !== $node->isMessageComplete()
139  );
140  }
_run(Socket\Node $node)
Definition: Connection.php:236

Here is the call graph for this function:

Hoa\Websocket\Client::run ( )

Override the parent run() method to open the connection.

Returns
void

Definition at line 117 of file Client.php.

118  {
119  $this->connect();
120 
121  return parent::run();
122  }

Here is the call graph for this function:

Hoa\Websocket\Client::setEndPoint (   $endPoint)
protected

Set end-point.

Parameters
string$endPointEnd-point.
Returns
string

Definition at line 247 of file Client.php.

248  {
249  $old = $this->_endPoint;
250  $this->_endPoint = $endPoint;
251 
252  return $old;
253  }

Here is the caller graph for this function:

Hoa\Websocket\Client::setHost (   $host)

Set host.

Parameters
string$hostHost.
Returns
string

Definition at line 295 of file Client.php.

296  {
297  $old = $this->_host;
298  $this->_host = $host;
299 
300  return $old;
301  }
Hoa\Websocket\Client::setResponse ( Http\Response  $response)

Set response (mainly parser).

Parameters
\Hoa\Http\Response$responseResponse.
Returns

Definition at line 271 of file Client.php.

272  {
273  $old = $this->_response;
274  $this->_response = $response;
275 
276  return $old;
277  }

Here is the caller graph for this function:

Member Data Documentation

Hoa\Websocket\Client::$_endPoint = null
protected

Definition at line 58 of file Client.php.

Hoa\Websocket\Client::$_host = null
protected

Definition at line 65 of file Client.php.

Hoa\Websocket\Client::$_response = null
protected

Definition at line 72 of file Client.php.


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