Hoa central
Server.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Websocket;
38 
39 use Hoa\Http;
40 use Hoa\Socket;
41 
50 class Server extends Connection
51 {
57  protected $_request = null;
58 
59 
60 
69  public function __construct(
70  Socket\Server $server,
71  Http\Request $request = null
72  ) {
73  parent::__construct($server);
74 
75  if (null === $request) {
76  $request = new Http\Request();
77  }
78 
79  $this->setRequest($request);
80 
81  return;
82  }
83 
90  protected function doHandshake()
91  {
92  $connection = $this->getConnection();
93  $buffer = $connection->read(2048);
94  $request = $this->getRequest();
95  $request->parse($buffer);
96 
97  // Rfc6455.
98  try {
99  $rfc6455 = new Protocol\Rfc6455($connection);
100  $rfc6455->doHandshake($request);
101  $connection->getCurrentNode()->setProtocolImplementation($rfc6455);
102  } catch (Exception\BadProtocol $e) {
103  unset($rfc6455);
104 
105  // Hybi00.
106  try {
107  $hybi00 = new Protocol\Hybi00($connection);
108  $hybi00->doHandshake($request);
109  $connection->getCurrentNode()->setProtocolImplementation($hybi00);
110  } catch (Exception\BadProtocol $e) {
111  unset($hybi00);
112  $connection->disconnect();
113 
114  throw new Exception\BadProtocol('All protocol failed.', 1);
115  }
116  }
117 
118  return;
119  }
120 
127  public function setRequest(Http\Request $request)
128  {
129  $old = $this->_request;
130  $this->_request = $request;
131 
132  return $old;
133  }
134 
140  public function getRequest()
141  {
142  return $this->_request;
143  }
144 }
__construct(Socket\Server $server, Http\Request $request=null)
Definition: Server.php:69
setRequest(Http\Request $request)
Definition: Server.php:127