Wrapped WebSocket connection data in object

Trying to prevent accidental parameter clobbering
This commit is contained in:
Chris Boden 2011-11-14 17:05:25 -05:00
parent 3363c08f36
commit ec607090d6
2 changed files with 10 additions and 10 deletions

View File

@ -11,7 +11,6 @@ use Ratchet\Application\WebSocket\Util\HTTP;
/** /**
* The adapter to handle WebSocket requests/responses * The adapter to handle WebSocket requests/responses
* This is a mediator between the Server and your application to handle real-time messaging through a web browser * This is a mediator between the Server and your application to handle real-time messaging through a web browser
* Sets 2 parameters on a Connection: handhsake_complete (bool) and websocket_version (Version\VersionInterface)
* @link http://ca.php.net/manual/en/ref.http.php * @link http://ca.php.net/manual/en/ref.http.php
* @todo Make sure this works both ways (client/server) as stack needs to exist on client for framing * @todo Make sure this works both ways (client/server) as stack needs to exist on client for framing
* @todo Learn about closing the socket. A message has to be sent prior to closing - does the message get sent onClose event or CloseConnection command? * @todo Learn about closing the socket. A message has to be sent prior to closing - does the message get sent onClose event or CloseConnection command?
@ -40,7 +39,7 @@ class App implements ApplicationInterface, ConfiguratorInterface {
public function __construct(ApplicationInterface $app = null) { public function __construct(ApplicationInterface $app = null) {
if (null === $app) { if (null === $app) {
throw new \UnexpectedValueException("WebSocket requires an application to run off of"); throw new \UnexpectedValueException("WebSocket requires an application to run");
} }
$this->_app = $app; $this->_app = $app;
@ -62,17 +61,18 @@ class App implements ApplicationInterface, ConfiguratorInterface {
} }
public function onOpen(Connection $conn) { public function onOpen(Connection $conn) {
$conn->handshake_complete = false; $conn->WebSocket = new \stdClass;
$conn->WebSocket->handshake = false;
} }
public function onRecv(Connection $from, $msg) { public function onRecv(Connection $from, $msg) {
if (true !== $from->handshake_complete) { if (true !== $from->WebSocket->handshake) {
// need buffer checks in here // need buffer checks in here
$from->websocket_version = $this->getVersion($msg); $from->WebSocket->version = $this->getVersion($msg);
$response = $from->websocket_version->handshake($msg); $response = $from->WebSocket->version->handshake($msg);
$from->handshake_complete = true; $from->WebSocket->handshake = true;
if (is_array($response)) { if (is_array($response)) {
$header = ''; $header = '';
@ -97,7 +97,7 @@ class App implements ApplicationInterface, ConfiguratorInterface {
// buffer! // buffer!
$msg = $from->websocket_version->unframe($msg); $msg = $from->WebSocket->version->unframe($msg);
if (is_array($msg)) { // temporary if (is_array($msg)) { // temporary
$msg = $msg['payload']; $msg = $msg['payload'];
} }
@ -126,7 +126,7 @@ class App implements ApplicationInterface, ConfiguratorInterface {
*/ */
protected function prepareCommand(CommandInterface $command = null) { protected function prepareCommand(CommandInterface $command = null) {
if ($command instanceof SendMessage) { if ($command instanceof SendMessage) {
$version = $command->getConnection()->websocket_version; $version = $command->getConnection()->WebSocket->version;
return $command->setMessage($version->frame($command->getMessage())); return $command->setMessage($version->frame($command->getMessage()));
} }

View File

@ -49,7 +49,7 @@ class Connection {
* @return mixed * @return mixed
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function __get($name) { public function &__get($name) {
if (!isset($this->_data[$name])) { if (!isset($this->_data[$name])) {
throw new \InvalidArgumentException("Attribute '{$name}' not found in Connection {$this->getID()}"); throw new \InvalidArgumentException("Attribute '{$name}' not found in Connection {$this->getID()}");
} }