Hoa central
Hybi00.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Websocket\Protocol;
38 
39 use Hoa\Http;
40 use Hoa\Websocket;
41 
50 class Hybi00 extends Generic
51 {
59  public function doHandshake(Http\Request $request)
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) {
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  }
96 
102  public function readFrame()
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  }
131 
141  public function writeFrame(
142  $message,
143  $opcode = -1,
144  $end = true,
145  $mask = false
146  ) {
147  return $this->_connection->writeAll(
148  chr(0) . $message . chr(255)
149  );
150  }
151 
161  public function send($message, $opcode = -1, $end = true, $mask = false)
162  {
163  $this->writeFrame($message);
164 
165  return;
166  }
167 
178  public function close(
179  $code = Websocket\Connection::CLOSE_NORMAL,
180  $reason = null,
181  $mask = false
182  ) {
183  return;
184  }
185 }
close($code=Websocket\Connection::CLOSE_NORMAL, $reason=null, $mask=false)
Definition: Hybi00.php:178
doHandshake(Http\Request $request)
Definition: Hybi00.php:59
writeFrame($message, $opcode=-1, $end=true, $mask=false)
Definition: Hybi00.php:141
send($message, $opcode=-1, $end=true, $mask=false)
Definition: Hybi00.php:161