Hoa central
Public Member Functions | List of all members
Hoa\Websocket\Protocol\Hybi00 Class Reference
Inheritance diagram for Hoa\Websocket\Protocol\Hybi00:

Public Member Functions

 doHandshake (Http\Request $request)
 
 readFrame ()
 
 writeFrame ($message, $opcode=-1, $end=true, $mask=false)
 
 send ($message, $opcode=-1, $end=true, $mask=false)
 
 close ($code=Websocket\Connection::CLOSE_NORMAL, $reason=null, $mask=false)
 
- Public Member Functions inherited from Hoa\Websocket\Protocol\Generic
 __construct (Socket\Connection $connection)
 
 doHandshake (Http\Request $request)
 
 readFrame ()
 
 writeFrame ($message, $opcode=Websocket\Connection::OPCODE_TEXT_FRAME, $end=true, $mask=false)
 
 send ($message, $opcode=Websocket\Connection::OPCODE_TEXT_FRAME, $end=true, $mask=false)
 
 close ($code=Websocket\Connection::CLOSE_NORMAL, $reason=null, $mask=false)
 

Additional Inherited Members

- Protected Attributes inherited from Hoa\Websocket\Protocol\Generic
 $_connection = null
 

Detailed Description

Class .

Protocol implementation: draft-ietf-hybi-thewebsocketprotocol-00.

Definition at line 50 of file Hybi00.php.

Member Function Documentation

Hoa\Websocket\Protocol\Hybi00::close (   $code = Websocket\Connection::CLOSE_NORMAL,
  $reason = null,
  $mask = false 
)

Close a specific node/connection.

Parameters
int$codeCode (please, see ::CLOSE_* constants).
string$reasonReason.
bool$maskWhether the message will be masked or not.
Returns
void

Definition at line 178 of file Hybi00.php.

182  {
183  return;
184  }
Hoa\Websocket\Protocol\Hybi00::doHandshake ( Http\Request  $request)

Do the handshake.

Parameters
\Hoa\Http\Request$requestRequest.
Returns
void
Exceptions

Definition at line 59 of file Hybi00.php.

60  {
61  $key1 = $request['sec-websocket-key1'];
62  $key2 = $request['sec-websocket-key2'];
63  $key3 = $request->getBody();
64  $location = $request['host'] . $request->getUrl();
65  $keynumb1 = (float) preg_replace('#[^0-9]#', '', $key1);
66  $keynumb2 = (float) preg_replace('#[^0-9]#', '', $key2);
67 
68  $spaces1 = substr_count($key1, ' ');
69  $spaces2 = substr_count($key2, ' ');
70 
71  if (0 === $spaces1 || 0 === $spaces2) {
72  throw new Websocket\Exception\BadProtocol(
73  'Header Sec-WebSocket-Key: %s is illegal.',
74  0
75  );
76  }
77 
78  $part1 = pack('N', (int) ($keynumb1 / $spaces1));
79  $part2 = pack('N', (int) ($keynumb2 / $spaces2));
80  $challenge = $part1 . $part2 . $key3;
81  $response = md5($challenge, true);
82 
83  $this->_connection->writeAll(
84  'HTTP/1.1 101 WebSocket Protocol Handshake' . "\r\n" .
85  'Upgrade: WebSocket' . "\r\n" .
86  'Connection: Upgrade' . "\r\n" .
87  'Sec-WebSocket-Origin: ' . $request['origin'] . "\r\n" .
88  'Sec-WebSocket-Location: ws://' . $location . "\r\n" .
89  "\r\n" .
90  $response . "\r\n"
91  );
92  $this->_connection->getCurrentNode()->setHandshake(SUCCEED);
93 
94  return;
95  }
Hoa\Websocket\Protocol\Hybi00::readFrame ( )

Read a frame.

Returns
array

Definition at line 102 of file Hybi00.php.

103  {
104  $buffer = $this->_connection->read(2048);
105  $length = strlen($buffer) - 2;
106 
107  if (empty($buffer)) {
108  return [
109  'fin' => 0x1,
110  'rsv1' => 0x0,
111  'rsv2' => 0x0,
112  'rsv3' => 0x0,
114  'mask' => 0x0,
115  'length' => 0,
116  'message' => null
117  ];
118  }
119 
120  return [
121  'fin' => 0x1,
122  'rsv1' => 0x0,
123  'rsv2' => 0x0,
124  'rsv3' => 0x0,
126  'mask' => 0x0,
127  'length' => $length,
128  'message' => substr($buffer, 1, $length)
129  ];
130  }
Hoa\Websocket\Protocol\Hybi00::send (   $message,
  $opcode = -1,
  $end = true,
  $mask = false 
)

Send a message to a node (if not specified, current node).

Parameters
string$messageMessage.
int$opcodeOpcode.
bool$endWhether it is the last frame of the message.
bool$maskWhether the message will be masked or not.
Returns
void

Definition at line 161 of file Hybi00.php.

162  {
163  $this->writeFrame($message);
164 
165  return;
166  }
writeFrame($message, $opcode=-1, $end=true, $mask=false)
Definition: Hybi00.php:141

Here is the call graph for this function:

Hoa\Websocket\Protocol\Hybi00::writeFrame (   $message,
  $opcode = -1,
  $end = true,
  $mask = false 
)

Write a frame.

Parameters
string$messageMessage.
int$opcodeOpcode (useless here).
bool$endWhether it is the last frame of the message.
bool$maskWhether the message will be masked or not.
Returns
int

Definition at line 141 of file Hybi00.php.

146  {
147  return $this->_connection->writeAll(
148  chr(0) . $message . chr(255)
149  );
150  }

Here is the caller graph for this function:


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