From b8967b999af7473e9d0fd150d523120ddb492036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Mon, 6 Feb 2017 08:09:41 +0100 Subject: [PATCH 1/4] Update Socket component to v0.5 --- composer.json | 2 +- src/Ratchet/App.php | 12 ++++++------ src/Ratchet/Server/IoServer.php | 8 +++++--- tests/autobahn/bin/fuzzingserver-noutf8.php | 4 +--- tests/autobahn/bin/fuzzingserver.php | 4 +--- tests/unit/Server/IoServerTest.php | 5 ++--- 6 files changed, 16 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index 80a3aa56..d396d9f8 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ } , "require": { "php": ">=5.3.9" - , "react/socket": "^0.3 || ^0.4" + , "react/socket": "^0.5" , "guzzle/http": "^3.6" , "symfony/http-foundation": "^2.2|^3.0" , "symfony/routing": "^2.2|^3.0" diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index c52a2c78..b70fd4bc 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -76,8 +76,7 @@ public function __construct($httpHost = 'localhost', $port = 8080, $address = '1 $this->httpHost = $httpHost; $this->port = $port; - $socket = new Reactor($loop); - $socket->listen($port, $address); + $socket = new Reactor($address . ':' . $port, $loop); $this->routes = new RouteCollection; $this->_server = new IoServer(new HttpServer(new Router(new UrlMatcher($this->routes, new RequestContext))), $socket, $loop); @@ -85,13 +84,14 @@ public function __construct($httpHost = 'localhost', $port = 8080, $address = '1 $policy = new FlashPolicy; $policy->addAllowedAccess($httpHost, 80); $policy->addAllowedAccess($httpHost, $port); - $flashSock = new Reactor($loop); - $this->flashServer = new IoServer($policy, $flashSock); + if (80 == $port) { - $flashSock->listen(843, '0.0.0.0'); + $flashUri = '0.0.0.0:843'; } else { - $flashSock->listen(8843); + $flashUri = 8843; } + $flashSock = new Reactor($flashUri, $loop); + $this->flashServer = new IoServer($policy, $flashSock); } /** diff --git a/src/Ratchet/Server/IoServer.php b/src/Ratchet/Server/IoServer.php index 921c7b14..9cf07982 100644 --- a/src/Ratchet/Server/IoServer.php +++ b/src/Ratchet/Server/IoServer.php @@ -66,8 +66,7 @@ public function __construct(MessageComponentInterface $app, ServerInterface $soc */ public static function factory(MessageComponentInterface $component, $port = 80, $address = '0.0.0.0') { $loop = LoopFactory::create(); - $socket = new Reactor($loop); - $socket->listen($port, $address); + $socket = new Reactor($address . ':' . $port, $loop); return new static($component, $socket, $loop); } @@ -94,7 +93,10 @@ public function handleConnect($conn) { $conn->decor = new IoConnection($conn); $conn->decor->resourceId = (int)$conn->stream; - $conn->decor->remoteAddress = $conn->getRemoteAddress(); + $conn->decor->remoteAddress = trim( + parse_url('tcp://' . $conn->getRemoteAddress(), PHP_URL_HOST), + '[]' + ); $this->app->onOpen($conn->decor); diff --git a/tests/autobahn/bin/fuzzingserver-noutf8.php b/tests/autobahn/bin/fuzzingserver-noutf8.php index 5ce1cb46..0a92ba47 100644 --- a/tests/autobahn/bin/fuzzingserver-noutf8.php +++ b/tests/autobahn/bin/fuzzingserver-noutf8.php @@ -6,12 +6,10 @@ $impl = sprintf('React\EventLoop\%sLoop', $argc > 2 ? $argv[2] : 'StreamSelect'); $loop = new $impl; - $sock = new React\Socket\Server($loop); + $sock = new React\Socket\Server('0.0.0.0:' . $port, $loop); $web = new Ratchet\WebSocket\WsServer(new Ratchet\Server\EchoServer); $app = new Ratchet\Http\HttpServer($web); $web->setEncodingChecks(false); - $sock->listen($port, '0.0.0.0'); - $server = new Ratchet\Server\IoServer($app, $sock, $loop); $server->run(); diff --git a/tests/autobahn/bin/fuzzingserver.php b/tests/autobahn/bin/fuzzingserver.php index 093a5cf4..62e9bc68 100644 --- a/tests/autobahn/bin/fuzzingserver.php +++ b/tests/autobahn/bin/fuzzingserver.php @@ -6,10 +6,8 @@ $impl = sprintf('React\EventLoop\%sLoop', $argc > 2 ? $argv[2] : 'StreamSelect'); $loop = new $impl; - $sock = new React\Socket\Server($loop); + $sock = new React\Socket\Server('0.0.0.0:' . $port, $loop); $app = new Ratchet\Http\HttpServer(new Ratchet\WebSocket\WsServer(new Ratchet\Server\EchoServer)); - $sock->listen($port, '0.0.0.0'); - $server = new Ratchet\Server\IoServer($app, $sock, $loop); $server->run(); diff --git a/tests/unit/Server/IoServerTest.php b/tests/unit/Server/IoServerTest.php index 808098a1..ec54b5d0 100644 --- a/tests/unit/Server/IoServerTest.php +++ b/tests/unit/Server/IoServerTest.php @@ -20,10 +20,9 @@ public function setUp() { $this->app = $this->getMock('\\Ratchet\\MessageComponentInterface'); $loop = new StreamSelectLoop; - $this->reactor = new Server($loop); - $this->reactor->listen(0); + $this->reactor = new Server(0, $loop); - $this->port = $this->reactor->getPort(); + $this->port = parse_url('tcp://' . $this->reactor->getAddress(), PHP_URL_PORT); $this->server = new IoServer($this->app, $this->reactor, $loop); } From 22e500d02a99eee614beedb11e15479edb308d10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Fri, 10 Mar 2017 15:13:39 +0100 Subject: [PATCH 2/4] Fix event arguments --- src/Ratchet/Server/IoServer.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Ratchet/Server/IoServer.php b/src/Ratchet/Server/IoServer.php index 9cf07982..40316387 100644 --- a/src/Ratchet/Server/IoServer.php +++ b/src/Ratchet/Server/IoServer.php @@ -24,6 +24,7 @@ class IoServer { /** * Array of React event handlers * @var \SplFixedArray + * @deprecated exists BC only, now unused */ protected $handlers; @@ -53,9 +54,6 @@ public function __construct(MessageComponentInterface $app, ServerInterface $soc $socket->on('connection', array($this, 'handleConnect')); $this->handlers = new \SplFixedArray(3); - $this->handlers[0] = array($this, 'handleData'); - $this->handlers[1] = array($this, 'handleEnd'); - $this->handlers[2] = array($this, 'handleError'); } /** @@ -100,9 +98,16 @@ public function handleConnect($conn) { $this->app->onOpen($conn->decor); - $conn->on('data', $this->handlers[0]); - $conn->on('end', $this->handlers[1]); - $conn->on('error', $this->handlers[2]); + $that = $this; + $conn->on('data', function ($data) use ($conn, $that) { + $that->handleData($data, $conn); + }); + $conn->on('close', function () use ($conn, $that) { + $that->handleEnd($conn); + }); + $conn->on('error', function (\Exception $e) use ($conn, $that) { + $that->handleError($e, $conn); + }); } /** From e3a97b66618ce8d2b444b0b56496b0e8a4118b72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Mon, 17 Apr 2017 17:36:23 +0200 Subject: [PATCH 3/4] Forward-compatibility with Socket v0.7 and Socket v0.6 Socket v0.7 and v0.6 contain some major changes, but this does not affect usage within Ratchet, so it's actually compatible with all latest releases. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d396d9f8..a070212a 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ } , "require": { "php": ">=5.3.9" - , "react/socket": "^0.5" + , "react/socket": "^0.7 || ^0.6 || ^0.5" , "guzzle/http": "^3.6" , "symfony/http-foundation": "^2.2|^3.0" , "symfony/routing": "^2.2|^3.0" From a86be3c52696dc02a7c08c0a782b46f67518f21e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Wed, 19 Jul 2017 16:12:26 +0200 Subject: [PATCH 4/4] Forward-compatibility with future Socket v1.0 and Socket v0.8 Socket v0.8 only contains some minor breaking changes, which can be circumvented by ignoring URI schemes here. Future Socket v1.0 will not contain any BC breaks, so it's actually compatible with the last release. --- composer.json | 2 +- src/Ratchet/Server/IoServer.php | 5 +++-- tests/unit/Server/IoServerTest.php | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index a070212a..993e4c79 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ } , "require": { "php": ">=5.3.9" - , "react/socket": "^0.7 || ^0.6 || ^0.5" + , "react/socket": "^1.0 || ^0.8 || ^0.7 || ^0.6 || ^0.5" , "guzzle/http": "^3.6" , "symfony/http-foundation": "^2.2|^3.0" , "symfony/routing": "^2.2|^3.0" diff --git a/src/Ratchet/Server/IoServer.php b/src/Ratchet/Server/IoServer.php index 40316387..3fcf76c0 100644 --- a/src/Ratchet/Server/IoServer.php +++ b/src/Ratchet/Server/IoServer.php @@ -89,10 +89,11 @@ public function run() { */ public function handleConnect($conn) { $conn->decor = new IoConnection($conn); - $conn->decor->resourceId = (int)$conn->stream; + + $uri = $conn->getRemoteAddress(); $conn->decor->remoteAddress = trim( - parse_url('tcp://' . $conn->getRemoteAddress(), PHP_URL_HOST), + parse_url((strpos($uri, '://') === false ? 'tcp://' : '') . $uri, PHP_URL_HOST), '[]' ); diff --git a/tests/unit/Server/IoServerTest.php b/tests/unit/Server/IoServerTest.php index ec54b5d0..284fbde1 100644 --- a/tests/unit/Server/IoServerTest.php +++ b/tests/unit/Server/IoServerTest.php @@ -22,7 +22,8 @@ public function setUp() { $loop = new StreamSelectLoop; $this->reactor = new Server(0, $loop); - $this->port = parse_url('tcp://' . $this->reactor->getAddress(), PHP_URL_PORT); + $uri = $this->reactor->getAddress(); + $this->port = parse_url((strpos($uri, '://') === false ? 'tcp://' : '') . $uri, PHP_URL_PORT); $this->server = new IoServer($this->app, $this->reactor, $loop); }