Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic properties #1065

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/Ratchet/Server/IoConnection.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
<?php
namespace Ratchet\Server;
use Ratchet\ConnectionInterface;
use React\Socket\ConnectionInterface as ReactConn;
use Ratchet\WebSocket\ReactConnection;

/**
* {@inheritdoc}
*/
class IoConnection implements ConnectionInterface {
/**
* @var \React\Socket\ConnectionInterface
* @var ReactConnection
*/
protected $conn;

/**
* Explicitly define properties to prevent dynamic property deprecation notices
*/
public $resourceId;
public $remoteAddress;
public $httpHeadersReceived;
public $httpBuffer;
public $httpRequest;
public $WebSocket;

/**
* @param \React\Socket\ConnectionInterface $conn
* @param ReactConnection $conn
*/
public function __construct(ReactConn $conn) {
public function __construct(ReactConnection $conn) {
$this->conn = $conn;
}

Expand Down
82 changes: 44 additions & 38 deletions src/Ratchet/Server/IoServer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Ratchet\Server;
use Ratchet\MessageComponentInterface;
use Ratchet\WebSocket\ReactConnection;
use React\EventLoop\LoopInterface;
use React\Socket\ServerInterface;
use React\EventLoop\Factory as LoopFactory;
Expand Down Expand Up @@ -75,66 +76,71 @@ public function run() {
// @codeCoverageIgnoreEnd
}

/**
* Triggered when a new connection is received from React
* @param \React\Socket\ConnectionInterface $conn
*/
/**
* Triggered when a new connection is received from React
* @param \React\Socket\ConnectionInterface $conn
* @throws \Exception
*/
public function handleConnect($conn) {
$conn->decor = new IoConnection($conn);
$conn->decor->resourceId = (int)$conn->stream;
$reactConn = new ReactConnection($conn->stream, $this->loop);
$reactConn->decor = new IoConnection($reactConn);
$reactConn->decor->resourceId = (int)$reactConn->stream;

$uri = $conn->getRemoteAddress();
$conn->decor->remoteAddress = trim(
$uri = $reactConn->getRemoteAddress();
$reactConn->decor->remoteAddress = trim(
parse_url((strpos($uri, '://') === false ? 'tcp://' : '') . $uri, PHP_URL_HOST),
'[]'
);

$this->app->onOpen($conn->decor);
$this->app->onOpen($reactConn->decor);

$conn->on('data', function ($data) use ($conn) {
$this->handleData($data, $conn);
$conn->on('data', function ($data) use ($reactConn) {
$this->handleData($data, $reactConn);
});
$conn->on('close', function () use ($conn) {
$this->handleEnd($conn);
$conn->on('close', function () use ($reactConn) {
$this->handleEnd($reactConn);
});
$conn->on('error', function (\Exception $e) use ($conn) {
$this->handleError($e, $conn);
$conn->on('error', function (\Exception $e) use ($reactConn) {
$this->handleError($e, $reactConn);
});
}

/**
* Data has been received from React
* @param string $data
* @param \React\Socket\ConnectionInterface $conn
*/
public function handleData($data, $conn) {
/**
* Data has been received from React
* @param string $data
* @param ReactConnection $reactConn
* @throws \Exception
*/
public function handleData($data, $reactConn) {
try {
$this->app->onMessage($conn->decor, $data);
$this->app->onMessage($reactConn->decor, $data);
} catch (\Exception $e) {
$this->handleError($e, $conn);
$this->handleError($e, $reactConn);
}
}

/**
* A connection has been closed by React
* @param \React\Socket\ConnectionInterface $conn
*/
public function handleEnd($conn) {
/**
* A connection has been closed by React
* @param $reactConn
* @throws \Exception
*/
public function handleEnd($reactConn) {
try {
$this->app->onClose($conn->decor);
$this->app->onClose($reactConn->decor);
} catch (\Exception $e) {
$this->handleError($e, $conn);
$this->handleError($e, $reactConn);
}

unset($conn->decor);
unset($reactConn->decor);
}

/**
* An error has occurred, let the listening application know
* @param \Exception $e
* @param \React\Socket\ConnectionInterface $conn
*/
public function handleError(\Exception $e, $conn) {
$this->app->onError($conn->decor, $e);
/**
* An error has occurred, let the listening application know
* @param \Exception $e
* @param $reactConn
* @throws \Exception
*/
public function handleError(\Exception $e, $reactConn) {
$this->app->onError($reactConn->decor, $e);
}
}
14 changes: 14 additions & 0 deletions src/Ratchet/WebSocket/ReactConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Ratchet\WebSocket;
use React\Socket\Connection;
use React\EventLoop\LoopInterface;

class ReactConnection extends Connection {
public $decor;
public $stream;


public function __construct($stream, $loop) {
parent::__construct($stream, $loop);
}
}