Server building
This commit is contained in:
parent
45fb69d68b
commit
30ce6c0386
@ -27,6 +27,6 @@ class Server implements ProtocolInterface {
|
|||||||
|
|
||||||
public function attatchApplication(ApplicationInterface $app) {}
|
public function attatchApplication(ApplicationInterface $app) {}
|
||||||
|
|
||||||
public function run() {
|
public function run($address = '127.0.0.1', $port = 1025) {
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,16 +5,29 @@ use Ratchet\Protocol\ProtocolInterface;
|
|||||||
class Server implements ServerInterface {
|
class Server implements ServerInterface {
|
||||||
protected $_master = null;
|
protected $_master = null;
|
||||||
protected $_app = 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->_master = $socket;
|
||||||
|
$this->_debug = (boolean)$debug;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function attatchApplication(ApplicationInterface $app) {
|
public function attatchApplication(ApplicationInterface $app) {
|
||||||
$this->_app = $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)) {
|
if (!($this->_app instanceof ApplicationInterface)) {
|
||||||
throw new \RuntimeException("No application has been bound to the server");
|
throw new \RuntimeException("No application has been bound to the server");
|
||||||
}
|
}
|
||||||
@ -22,9 +35,21 @@ class Server implements ServerInterface {
|
|||||||
set_time_limit(0);
|
set_time_limit(0);
|
||||||
ob_implicit_flush();
|
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 {
|
do {
|
||||||
|
$changed = $this->_connections;
|
||||||
} while (true);
|
$num_changed = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL);
|
||||||
|
// foreach($changed as $)
|
||||||
|
|
||||||
|
} while (!$this->_debug);
|
||||||
|
|
||||||
// $this->_master->set_nonblock();
|
// $this->_master->set_nonblock();
|
||||||
// declare(ticks = 1);
|
// declare(ticks = 1);
|
||||||
}
|
}
|
||||||
|
@ -4,5 +4,5 @@ namespace Ratchet;
|
|||||||
interface ServerInterface {
|
interface ServerInterface {
|
||||||
function attatchApplication(ApplicationInterface $app);
|
function attatchApplication(ApplicationInterface $app);
|
||||||
|
|
||||||
function run();
|
function run($address = '127.0.0.1', $port = 1025);
|
||||||
}
|
}
|
@ -30,4 +30,13 @@ class ServerTest extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->_server->attatchApplication($app);
|
$this->_server->attatchApplication($app);
|
||||||
$this->assertAttributeEquals($app, '_app', $this->_server);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user