BC Break - Server::run parameters change

Updated IoServerComponent::run parameters

* $port is now first
* only $port is required
* SocketInterface is last and optional
This commit is contained in:
Chris Boden 2012-04-22 13:15:43 -04:00
parent ffabf09426
commit e06aa30096
4 changed files with 15 additions and 10 deletions

View File

@ -5,7 +5,7 @@
A PHP 5.3 (PSR-0 compliant) component library for serving sockets and building socket based applications.
Build up your application through simple interfaces using the decorator and command patterns.
Re-use your application without changing any of its code just by combining different components.
Ratchet's primary intention is to be used as a WebSocket server (and a client in 0.6).
Ratchet's primary intention is to be used as a WebSocket server.
##WebSockets
@ -14,7 +14,7 @@ Ratchet's primary intention is to be used as a WebSocket server (and a client in
##Requirements
Shell access is required and a dedicated (virtual) machine with root access is recommended.
Shell access is required and a dedicated machine with root access is recommended.
To avoid proxy/firewall blockage it's recommended WebSockets are run on port 80, which requires root access.
Note that you can not run two applications (Apache and Ratchet) on the same port, thus the requirement for a separate machine (for now).
@ -28,7 +28,7 @@ See https://github.com/cboden/Ratchet-examples for some out-of-the-box working d
Ideally, soon, web servers will start supporting WebSockets to some capacity and PHP will no longer need to run its self from the command line.
In theory, the server (like Nginx) would recognize the HTTP handshake request to upgrade the protocol to WebSockets and run/pass data through to a user
configured PHP file. When this happens, you can keep your script the same, just remove the IOServerComponent wrapper and maybe eventually the
WebSocket Application wrapper if the servers recognize the protocol message framing.
WebSocketComponent wrapper if the servers recognize the protocol message framing.
---
@ -86,7 +86,7 @@ class Chat implements MessageComponentInterface {
}
// Run the server application through the WebSocket protocol
$server = new Server(new WebSocket(new Chat));
$server->run(new Socket, '0.0.0.0', 80);
$server->run(80);
```
# php chat.php

View File

@ -2,6 +2,7 @@
namespace Ratchet\Component\Server;
use Ratchet\Component\MessageComponentInterface;
use Ratchet\Resource\Socket\SocketInterface;
use Ratchet\Resource\Socket\BSDSocket;
use Ratchet\Resource\ConnectionInterface;
use Ratchet\Resource\Connection;
use Ratchet\Resource\Command\CommandInterface;
@ -63,12 +64,17 @@ class IOServerComponent implements MessageComponentInterface {
/*
* Run the server infinitely
* @param Ratchet\Resource\Socket\SocketInterface
* @param mixed The address to listen for incoming connections on. "0.0.0.0" to listen from anywhere
* @param int The port to listen to connections on (make sure to run as root if < 1000)
* @param mixed The address to listen for incoming connections on. "0.0.0.0" to listen from anywhere
* @param Ratchet\Resource\Socket\SocketInterface
* @throws Ratchet\Exception
*/
public function run(SocketInterface $host, $address = '127.0.0.1', $port = 1025) {
public function run($port, $address = '0.0.0.0', SocketInterface $host = null) {
if (null === $host) {
$host = new BSDSocket;
$host->set_option(SOL_SOCKET, SO_REUSEADDR, 1);
}
$this->_connections[$host->getResource()] = new Connection($host);
$this->_resources[] = $host->getResource();

View File

@ -237,7 +237,6 @@ class BSDSocket implements SocketInterface {
$return = array();
foreach ($collection as $key => $socket) {
die("Checking if sock is instance of this: " . (int)($socket instanceof $this) . "\n");
$return[$key] = ($socket instanceof $this ? $socket->getResource() : $socket);
}

View File

@ -37,7 +37,7 @@ class IOServerComponentTest extends \PHPUnit_Framework_TestCase {
}
public function testOnOpenPassesClonedSocket() {
$this->_server->run($this->_catalyst);
$this->_server->run(1025, '127.0.0.1', $this->_catalyst);
$master = $this->getMasterConnection();
$this->_server->onOpen($master);
@ -47,7 +47,7 @@ class IOServerComponentTest extends \PHPUnit_Framework_TestCase {
}
public function testOnMessageSendsToApp() {
$this->_server->run($this->_catalyst);
$this->_server->run(1025, '127.0.0.1', $this->_catalyst);
$master = $this->getMasterConnection();
// todo, make FakeSocket better, set data in select, recv to pass data when called, then do this check