diff --git a/composer.json b/composer.json index 80a3aa56..993e4c79 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ } , "require": { "php": ">=5.3.9" - , "react/socket": "^0.3 || ^0.4" + , "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/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..3fcf76c0 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'); } /** @@ -66,8 +64,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); } @@ -92,15 +89,26 @@ public function run() { */ public function handleConnect($conn) { $conn->decor = new IoConnection($conn); - $conn->decor->resourceId = (int)$conn->stream; - $conn->decor->remoteAddress = $conn->getRemoteAddress(); + + $uri = $conn->getRemoteAddress(); + $conn->decor->remoteAddress = trim( + parse_url((strpos($uri, '://') === false ? 'tcp://' : '') . $uri, PHP_URL_HOST), + '[]' + ); $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); + }); } /** 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..284fbde1 100644 --- a/tests/unit/Server/IoServerTest.php +++ b/tests/unit/Server/IoServerTest.php @@ -20,10 +20,10 @@ 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(); + $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); }