diff --git a/lib/Ratchet/Protocol/WebSocket/Server.php b/lib/Ratchet/Protocol/WebSocket/Server.php index 067d2e4..b896111 100644 --- a/lib/Ratchet/Protocol/WebSocket/Server.php +++ b/lib/Ratchet/Protocol/WebSocket/Server.php @@ -27,6 +27,6 @@ class Server implements ProtocolInterface { public function attatchApplication(ApplicationInterface $app) {} - public function run() { + public function run($address = '127.0.0.1', $port = 1025) { } } \ No newline at end of file diff --git a/lib/Ratchet/Server.php b/lib/Ratchet/Server.php index 01748c2..68db6db 100644 --- a/lib/Ratchet/Server.php +++ b/lib/Ratchet/Server.php @@ -5,16 +5,29 @@ use Ratchet\Protocol\ProtocolInterface; class Server implements ServerInterface { protected $_master = null; protected $_app = null; + protected $_debug = false; - public function __construct(Socket $socket) { + protected $_connections = Array(); + + /** + * @param Ratchet\Socket + * @param boolean True, enables debug mode and the server doesn't infiniate loop + */ + public function __construct(Socket $socket, $debug = false) { $this->_master = $socket; + $this->_debug = (boolean)$debug; } public function attatchApplication(ApplicationInterface $app) { $this->_app = $app; } - public function run() { + /* + * @param mixed + * @param int + * @throws Ratchet\Exception + */ + public function run($address = '127.0.0.1', $port = 1025) { if (!($this->_app instanceof ApplicationInterface)) { throw new \RuntimeException("No application has been bound to the server"); } @@ -22,9 +35,21 @@ class Server implements ServerInterface { set_time_limit(0); ob_implicit_flush(); + if (false === ($this->_master->bind($address, (int)$port))) { // perhaps I should do some checks here... + throw new Exception(); + } + + if (false === ($this->_master->listen())) { + throw new Exception(); + } + do { - - } while (true); + $changed = $this->_connections; + $num_changed = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL); +// foreach($changed as $) + + } while (!$this->_debug); + // $this->_master->set_nonblock(); // declare(ticks = 1); } diff --git a/lib/Ratchet/ServerInterface.php b/lib/Ratchet/ServerInterface.php index a1c2585..0566126 100644 --- a/lib/Ratchet/ServerInterface.php +++ b/lib/Ratchet/ServerInterface.php @@ -4,5 +4,5 @@ namespace Ratchet; interface ServerInterface { function attatchApplication(ApplicationInterface $app); - function run(); + function run($address = '127.0.0.1', $port = 1025); } \ No newline at end of file diff --git a/tests/Ratchet/Tests/ServerTest.php b/tests/Ratchet/Tests/ServerTest.php index 9bc2db1..f271e92 100644 --- a/tests/Ratchet/Tests/ServerTest.php +++ b/tests/Ratchet/Tests/ServerTest.php @@ -30,4 +30,13 @@ class ServerTest extends \PHPUnit_Framework_TestCase { $this->_server->attatchApplication($app); $this->assertAttributeEquals($app, '_app', $this->_server); } + + public function testBindToInvalidAddress() { + $app = new TestApp(); + + $this->_server->attatchApplication($app); + $this->setExpectedException('\\Ratchet\\Exception'); + + $this->_server->run('la la la', 80); + } } \ No newline at end of file