diff --git a/.gitignore b/.gitignore index a7b14b2..a482d6b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /.idea -/phpunit.phar \ No newline at end of file +/phpunit.phar +/vendor +/composer.lock \ No newline at end of file diff --git a/SplClassLoader.php b/SplClassLoader.php deleted file mode 100644 index 712430d..0000000 --- a/SplClassLoader.php +++ /dev/null @@ -1,136 +0,0 @@ -register(); - * - * @author Jonathan H. Wage - * @author Roman S. Borschel - * @author Matthew Weier O'Phinney - * @author Kris Wallsmith - * @author Fabien Potencier - */ -class SplClassLoader -{ - private $_fileExtension = '.php'; - private $_namespace; - private $_includePath; - private $_namespaceSeparator = '\\'; - - /** - * Creates a new SplClassLoader that loads classes of the - * specified namespace. - * - * @param string $ns The namespace to use. - * @param string $includePath - */ - public function __construct($ns = null, $includePath = null) - { - $this->_namespace = $ns; - $this->_includePath = $includePath; - } - - /** - * Sets the namespace separator used by classes in the namespace of this class loader. - * - * @param string $sep The separator to use. - */ - public function setNamespaceSeparator($sep) - { - $this->_namespaceSeparator = $sep; - } - - /** - * Gets the namespace seperator used by classes in the namespace of this class loader. - * - * @return string - */ - public function getNamespaceSeparator() - { - return $this->_namespaceSeparator; - } - - /** - * Sets the base include path for all class files in the namespace of this class loader. - * - * @param string $includePath - */ - public function setIncludePath($includePath) - { - $this->_includePath = $includePath; - } - - /** - * Gets the base include path for all class files in the namespace of this class loader. - * - * @return string $includePath - */ - public function getIncludePath() - { - return $this->_includePath; - } - - /** - * Sets the file extension of class files in the namespace of this class loader. - * - * @param string $fileExtension - */ - public function setFileExtension($fileExtension) - { - $this->_fileExtension = $fileExtension; - } - - /** - * Gets the file extension of class files in the namespace of this class loader. - * - * @return string $fileExtension - */ - public function getFileExtension() - { - return $this->_fileExtension; - } - - /** - * Installs this class loader on the SPL autoload stack. - */ - public function register() - { - spl_autoload_register(array($this, 'loadClass')); - } - - /** - * Uninstalls this class loader from the SPL autoloader stack. - */ - public function unregister() - { - spl_autoload_unregister(array($this, 'loadClass')); - } - - /** - * Loads the given class or interface. - * - * @param string $className The name of the class to load. - * @return void - */ - public function loadClass($className) - { - if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) { - $fileName = ''; - if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) { - $namespace = substr($className, 0, $lastNsPos); - $className = substr($className, $lastNsPos + 1); - $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; - } - $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension; - - require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName; - } - } -} \ No newline at end of file diff --git a/composer.json b/composer.json index d5b465f..b9838ab 100644 --- a/composer.json +++ b/composer.json @@ -12,5 +12,8 @@ "psr-0": { "Devristo\\Phpws\\": "src/" } + }, + "require": { + "zendframework/zend-log": "2.*" } -} \ No newline at end of file +} diff --git a/demo.php b/demo.php index 3bc673d..438d631 100644 --- a/demo.php +++ b/demo.php @@ -1,10 +1,7 @@ #!/php -q register(); +require_once(__DIR__."/vendor/autoload.php"); // Run from command prompt > php demo.php @@ -26,13 +23,13 @@ class DemoEchoHandler extends WebSocketUriHandler { public function onMessage(IWebSocketConnection $user, IWebSocketMessage $msg) { - $this->say("[ECHO] " . strlen($msg->getData()) . " bytes"); + $this->logger->notice("[ECHO] " . strlen($msg->getData()) . " bytes"); // Echo $user->sendMessage($msg); } public function onAdminMessage(IWebSocketConnection $user, IWebSocketMessage $obj) { - $this->say("[DEMO] Admin TEST received!"); + $this->logger->notice("[DEMO] Admin TEST received!"); $frame = WebSocketFrame::create(WebSocketOpcode::PongFrame); $user->sendFrame($frame); @@ -53,35 +50,36 @@ class DemoSocketServer implements IWebSocketServerObserver { protected $server; public function __construct() { - $this->server = new WebSocketServer("tcp://0.0.0.0:12345", 'superdupersecretkey'); + $logger = new \Zend\Log\Logger(); + $logger->addWriter(new Zend\Log\Writer\Stream("php://output")); + + $this->logger = $logger; + + $this->server = new WebSocketServer("tcp://0.0.0.0:12345", $logger); $this->server->addObserver($this); - $this->server->addUriHandler("echo", new DemoEchoHandler()); + $this->server->addUriHandler("echo", new DemoEchoHandler($logger)); } public function onConnect(IWebSocketConnection $user) { - $this->say("[DEMO] {$user->getId()} connected"); + $this->logger->notice("[DEMO] {$user->getId()} connected"); } public function onMessage(IWebSocketConnection $user, IWebSocketMessage $msg) { - //$this->say("[DEMO] {$user->getId()} says '{$msg->getData()}'"); + //$this->logger->notice("[DEMO] {$user->getId()} says '{$msg->getData()}'"); } public function onDisconnect(IWebSocketConnection $user) { - $this->say("[DEMO] {$user->getId()} disconnected"); + $this->logger->notice("[DEMO] {$user->getId()} disconnected"); } public function onAdminMessage(IWebSocketConnection $user, IWebSocketMessage $msg) { - $this->say("[DEMO] Admin Message received!"); + $this->logger->notice("[DEMO] Admin Message received!"); $frame = WebSocketFrame::create(WebSocketOpcode::PongFrame); $user->sendFrame($frame); } - public function say($msg) { - echo "$msg \r\n"; - } - public function run() { $this->server->run(); } diff --git a/demo_ssl.php b/demo_ssl.php index f462b60..e1039e7 100644 --- a/demo_ssl.php +++ b/demo_ssl.php @@ -1,10 +1,7 @@ #!/php -q register(); +require_once("vendor/autoload.php"); // Run from command prompt > php demo.php @@ -26,13 +23,13 @@ class DemoSslEchoHandler extends WebSocketUriHandler { public function onMessage(IWebSocketConnection $user, IWebSocketMessage $msg) { - $this->say("[ECHO] {$msg->getData()}"); + $this->logger->notice("[ECHO] {$msg->getData()}"); // Echo $user->sendMessage($msg); } public function onAdminMessage(IWebSocketConnection $user, IWebSocketMessage $obj) { - $this->say("[DEMO] Admin TEST received!"); + $this->logger->notice("[DEMO] Admin TEST received!"); $frame = WebSocketFrame::create(WebSocketOpcode::PongFrame); $user->sendFrame($frame); @@ -53,10 +50,15 @@ class DemoSslSocketServer implements IWebSocketServerObserver { protected $server; public function __construct() { - $this->server = new WebSocketServer("ssl://0.0.0.0:12345"); + $logger = new \Zend\Log\Logger(); + $logger->addWriter(new Zend\Log\Writer\Stream("php://output")); + + $this->logger = $logger; + + $this->server = new WebSocketServer("ssl://0.0.0.0:12345", $logger); $this->server->addObserver($this); - $this->server->addUriHandler("echo", new DemoSslEchoHandler()); + $this->server->addUriHandler("echo", new DemoSslEchoHandler($logger)); $this->setupSSL(); } @@ -78,28 +80,24 @@ public function setupSSL() { } public function onConnect(IWebSocketConnection $user) { - $this->say("[DEMO] {$user->getId()} connected"); + $this->logger->notice("[DEMO] {$user->getId()} connected"); } public function onMessage(IWebSocketConnection $user, IWebSocketMessage $msg) { - $this->say("[DEMO] {$user->getId()} says '{$msg->getData()}'"); + $this->logger->notice("[DEMO] {$user->getId()} says '{$msg->getData()}'"); } public function onDisconnect(IWebSocketConnection $user) { - $this->say("[DEMO] {$user->getId()} disconnected"); + $this->logger->notice("[DEMO] {$user->getId()} disconnected"); } public function onAdminMessage(IWebSocketConnection $user, IWebSocketMessage $msg) { - $this->say("[DEMO] Admin Message received!"); + $this->logger->notice("[DEMO] Admin Message received!"); $frame = WebSocketFrame::create(WebSocketOpcode::PongFrame); $user->sendFrame($frame); } - public function say($msg) { - echo "$msg \r\n"; - } - public function run() { $this->server->run(); } diff --git a/src/Devristo/Phpws/Protocol/WebSocketConnection.php b/src/Devristo/Phpws/Protocol/WebSocketConnection.php index dcfbd3b..b285570 100644 --- a/src/Devristo/Phpws/Protocol/WebSocketConnection.php +++ b/src/Devristo/Phpws/Protocol/WebSocketConnection.php @@ -11,12 +11,19 @@ use Devristo\Phpws\Framing\IWebSocketFrame; use Devristo\Phpws\Messaging\IWebSocketMessage; use Devristo\Phpws\Protocol\WebSocketStream; +use Zend\Log\LoggerAwareInterface; +use Zend\Log\LoggerInterface; -abstract class WebSocketConnection implements IWebSocketConnection +abstract class WebSocketConnection implements IWebSocketConnection, LoggerAwareInterface { protected $_headers = array(); + /** + * @var LoggerInterface + */ + protected $logger; + /** * * @var WebSocketStream @@ -34,12 +41,12 @@ public function __construct(WebSocketStream $socket, array $headers) public function getIp() { - return stream_socket_get_name($this->_socket->getResource(), true); + return stream_socket_get_name($this->_socket->getSocket(), true); } public function getId() { - return (int)$this->_socket->getResource(); + return (int)$this->_socket->getSocket(); } public function sendFrame(IWebSocketFrame $frame) @@ -154,4 +161,7 @@ public function getSocket() return $this->_socket; } + public function setLogger(LoggerInterface $logger){ + $this->logger = $logger; + } } \ No newline at end of file diff --git a/src/Devristo/Phpws/Protocol/WebSocketConnectionFactory.php b/src/Devristo/Phpws/Protocol/WebSocketConnectionFactory.php index e95290e..61532fa 100644 --- a/src/Devristo/Phpws/Protocol/WebSocketConnectionFactory.php +++ b/src/Devristo/Phpws/Protocol/WebSocketConnectionFactory.php @@ -13,27 +13,30 @@ use Devristo\Phpws\Protocol\WebSocketConnectionHybi; use Devristo\Phpws\Protocol\WebSocketConnectionRole; use Devristo\Phpws\Protocol\WebSocketStream; +use Zend\Log\LoggerInterface; class WebSocketConnectionFactory { - public static function fromSocketData(WebSocketStream $socket, $data) + public static function fromSocketData(WebSocketStream $socket, $data, LoggerInterface $logger) { $headers = self::parseHeaders($data); if (isset($headers['Sec-Websocket-Key1'])) { $s = new WebSocketConnectionHixie($socket, $headers, $data); + $s->setLogger($logger); $s->sendHandshakeResponse(); } else if (strpos($data, '') === 0) { $s = new WebSocketConnectionFlash($socket, $data); + $s->setLogger($logger); } else { $s = new WebSocketConnectionHybi($socket, $headers); + $s->setLogger($logger); $s->sendHandshakeResponse(); } $s->setRole(WebSocketConnectionRole::SERVER); - return $s; } diff --git a/src/Devristo/Phpws/Protocol/WebSocketConnectionHybi.php b/src/Devristo/Phpws/Protocol/WebSocketConnectionHybi.php index b5584fa..a35b2d9 100644 --- a/src/Devristo/Phpws/Protocol/WebSocketConnectionHybi.php +++ b/src/Devristo/Phpws/Protocol/WebSocketConnectionHybi.php @@ -40,7 +40,7 @@ public function sendHandshakeResponse() $this->_socket->write($response); - echo "HYBI Response SENT!\n"; + $this->logger->debug("HYBI Response SENT!"); } private static function calcHybiResponse($challenge) diff --git a/src/Devristo/Phpws/Protocol/WebSocketStream.php b/src/Devristo/Phpws/Protocol/WebSocketStream.php index 72d4a52..5c2b231 100644 --- a/src/Devristo/Phpws/Protocol/WebSocketStream.php +++ b/src/Devristo/Phpws/Protocol/WebSocketStream.php @@ -6,13 +6,18 @@ use Devristo\Phpws\Protocol\IWebSocketConnection; use Devristo\Phpws\Protocol\WebSocketConnectionFactory; use Devristo\Phpws\Protocol\WebSocketConnectionFlash; +use Devristo\Phpws\Server\ISocketStream; use Exception; +use Zend\Log\LoggerAwareInterface; +use Zend\Log\LoggerInterface; -class WebSocketStream +class WebSocketStream implements ISocketStream, LoggerAwareInterface { private $_socket = null; + protected $logger; + /** * * @var IWebSocketConnection @@ -67,7 +72,7 @@ public function onMessage(IWebSocketMessage $m) public function establishConnection($data) { - $this->_connection = WebSocketConnectionFactory::fromSocketData($this, $data); + $this->_connection = WebSocketConnectionFactory::fromSocketData($this, $data, $this->logger); if ($this->_connection instanceof WebSocketConnectionFlash) return; @@ -87,7 +92,7 @@ public function write($data) } } - public function mustWrite() + public function requestsWrite() { return strlen($this->_writeBuffer); } @@ -138,7 +143,7 @@ public function close() } } - public function getResource() + public function getSocket() { return $this->_socket; } @@ -157,4 +162,18 @@ public function addObserver(WebSocketObserver $s) $this->_observers[] = $s; } + public function acceptConnection() + { + throw new \BadMethodCallException(); + } + + public function isServer() + { + return false; + } + + public function setLogger(LoggerInterface $logger) + { + $this->logger = $logger; + } } \ No newline at end of file diff --git a/src/Devristo/Phpws/Server/ISocketStream.php b/src/Devristo/Phpws/Server/ISocketStream.php new file mode 100644 index 0000000..a1b6312 --- /dev/null +++ b/src/Devristo/Phpws/Server/ISocketStream.php @@ -0,0 +1,20 @@ +streams = new \SplObjectStorage(); + + $this->_logger = $logger; + } + + public function attachStream(ISocketStream $stream){ + $this->streams->attach($stream); + $this->_logger->info("Attaching stream"); + } + + public function detachStream(ISocketStream $stream){ + $this->streams->detach($stream); + $this->_logger->info("Detaching stream"); + } + + public function run(){ + $this->_eventLoop(); + } + + public function getStreams(){ + return $this->streams; + } + + public function getSockets(){ + $sockets = array(); + + foreach($this->streams as $stream) + $sockets[] = $stream->getSocket(); + + return $sockets; + } + + private function getWriteResources(){ + $wantsToWrite = array(); + foreach($this->getStreams() as $stream){ + if($stream->requestsWrite()) + $wantsToWrite[] = $stream->getSocket(); + } + + return $wantsToWrite; + } + + private function getStreamByResource($resource){ + foreach($this->streams as $stream) + if($stream->getSocket() == $resource) + return $stream; + + return null; + } + + protected function _eventLoop(){ + while (true) { + clearstatcache(); + gc_collect_cycles(); + + $changed = $this->getSockets(); + $write = $this->getWriteResources(); + $except = null; + + if (@stream_select($changed, $write, $except, null) === false) { + $this->_logger->err("Stream select has failed. You might need to restart the server if it happens again"); + break; + } + $this->_logger->debug("Streams selected"); + + + foreach ($changed as $resource) { + $stream = $this->getStreamByResource($resource); + + if(!$stream){ + $this->_logger->err("Cannot find ISocketStream using this socket, skipping it"); + @fclose($resource); + continue; + } + + + if ($stream->isServer()) { + $stream->acceptConnection(); + } else { + $buffer = fread($resource, 8192); + + // If read returns false, close the stream and continue with the next socket + if ($buffer === false) { + $stream->close(); + // Skip to next stream + continue; + } + + $bytes = strlen($buffer); + + if ($bytes === 0) { + $stream->close(); + } else if ($stream != null) { + $stream->onData($buffer); + } + } + } + + if (is_array($write)) { + foreach ($write as $resource) { + $stream = $this->getStreamByResource($resource); + + if(!$stream){ + $this->_logger->err("Cannot find ISocketStream using this socket, skipping it"); + @fclose($resource); + continue; + } + + $stream->mayWrite(); + } + } + } + } +} \ No newline at end of file diff --git a/src/Devristo/Phpws/Server/UriHandler/WebSocketUriHandler.php b/src/Devristo/Phpws/Server/UriHandler/WebSocketUriHandler.php index 04f2274..9705d3a 100644 --- a/src/Devristo/Phpws/Server/UriHandler/WebSocketUriHandler.php +++ b/src/Devristo/Phpws/Server/UriHandler/WebSocketUriHandler.php @@ -6,6 +6,7 @@ use Devristo\Phpws\Protocol\IWebSocketConnection; use Devristo\Phpws\Server\WebSocketServer; use SplObjectStorage; +use Zend\Log\LoggerInterface; abstract class WebSocketUriHandler implements IWebSocketUriHandler { @@ -24,9 +25,15 @@ abstract class WebSocketUriHandler implements IWebSocketUriHandler */ protected $server; - public function __construct() + /** + * @var LoggerInterface + */ + protected $logger; + + public function __construct($logger) { $this->users = new SplObjectStorage(); + $this->logger = $logger; } public function addConnection(IWebSocketConnection $user) @@ -45,11 +52,6 @@ public function setServer(WebSocketServer $server) $this->server = $server; } - public function say($msg = '') - { - return $this->server->say($msg); - } - public function send(IWebSocketConnection $client, $str) { return $client->sendString($str); diff --git a/src/Devristo/Phpws/Server/WebSocketServer.php b/src/Devristo/Phpws/Server/WebSocketServer.php index 560f5b2..d5c04e8 100644 --- a/src/Devristo/Phpws/Server/WebSocketServer.php +++ b/src/Devristo/Phpws/Server/WebSocketServer.php @@ -10,13 +10,14 @@ use Devristo\Phpws\Server\UriHandler\IWebSocketUriHandler; use Exception; use SplObjectStorage; +use Zend\Log\LoggerInterface; /** * WebSocketServer * * @author Chris */ -class WebSocketServer implements WebSocketObserver +class WebSocketServer implements WebSocketObserver, ISocketStream { protected $master; @@ -62,19 +63,17 @@ class WebSocketServer implements WebSocketObserver * Must be implemented by all extending classes * * @param $url - * @param bool $showHeaders + * @param null $logger */ - public function __construct($url, $showHeaders = false) + public function __construct($url, LoggerInterface $logger) { - define("WS_DEBUG_HEADER", $showHeaders); - - $this->_url = $url; - $this->sockets = new SplObjectStorage(); $this->_connections = new SplObjectStorage(); $this->_context = stream_context_create(); + $this->_logger = $logger; + $this->_server = new SocketServer($logger); } public function getStreamContext() @@ -133,124 +132,34 @@ public function run() $this->master = stream_socket_server($this->_url, $errno, $err, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $this->_context); - $this->say("PHP WebSocket Server"); - $this->say("========================================"); - $this->say("Server Started : " . date('Y-m-d H:i:s')); - $this->say("Listening on : " . $this->_url); - $this->say("========================================"); + $this->_logger->notice(sprintf("phpws listening on %s", $this->_url)); if ($this->master == false) { - $this->say("Error: $err"); + $this->_logger->err("Error: $err"); return; } - $this->sockets->attach(new WebSocketStream($this, $this->master)); - - - while (true) { - - clearstatcache(); - // Garbage Collection (PHP >= 5.3) - if (function_exists('gc_collect_cycles')) { - gc_collect_cycles(); - } - - //$this->debug("Blocking on socket_select()"); - // Retreive sockets which are 'Changed' - $changed = $this->getResources(); - $write = $this->getWriteStreams(); - $except = null; - - if (@stream_select($changed, $write, $except, null) === false) { - $this->say("Select failed!"); - break; - } - - - //$this->debug("Socket selected"); - - - foreach ($changed as $resource) { - if ($resource == $this->master) { - $this->acceptSocket(); - } else { - $buffer = fread($resource, 8192); - - $socket = $this->getSocketByResource($resource); - - // If read returns false, close the stream and continue with the next socket - if ($buffer === false) { - $socket->close(); - // Skip to next stream - continue; - } - - $bytes = strlen($buffer); - - if ($bytes === 0) { - $socket->close(); - } else if ($socket != null) { - $socket->onData($buffer); - } - } - } - - if (is_array($write)) { - foreach ($write as $s) { - $o = $this->getSocketByResource($s); - if ($o != null) - $o->mayWrite(); - } - } - - - //$this->debug('Number of users connected: '.count($this->getConnections())); - $this->purgeUsers(); - } + $this->_server->attachStream($this); + $this->_server->run(); } - private function acceptSocket() + public function acceptConnection() { try { $client = stream_socket_accept($this->master); stream_set_blocking($client, 0); if ($client === false) { - echo 'socket_accept() failed\n'; + $this->_logger->warn("Failed to accept client connection"); } + $stream = new WebSocketStream($this, $client); + $stream->setLogger($this->_logger); + $this->_server->attachStream($stream); - $this->sockets->attach(new WebSocketStream($this, $client)); - - $this->debug("Socket accepted"); + $this->_logger->info("WebSocket client accepted"); } catch (Exception $e) { - $this->say($e); - } - } - - /** - * - * @param resource $res - * @return \Devristo\Phpws\Protocol\WebSocketStream - */ - private function getSocketByResource($res) - { - foreach ($this->sockets as $socket) { - if ($socket->getResource() == $res) - return $socket; - } - - return null; - } - - private function getResources() - { - $resources = array(); - - foreach ($this->sockets as $socket) { - $resources[] = $socket->getResource(); + $this->_logger->crit("Failed to accept client connection"); } - - return $resources; } public function addObserver(IWebSocketServerObserver $o) @@ -277,7 +186,7 @@ public function addUriHandler($script, IWebSocketUriHandler $handler) */ protected function dispatchMessage(IWebSocketConnection $user, IWebSocketMessage $msg) { - $this->debug("dispatchMessage"); + $this->_logger->debug("Dispatching message to URI handlers and Observers"); if (array_key_exists($this->_connections[$user], $this->uriHandlers)) { $this->uriHandlers[$this->_connections[$user]]->onMessage($user, $msg); @@ -308,7 +217,7 @@ protected function addConnectionToUriHandler(IWebSocketConnection $user, $uri) if (isset($url['path']) == false) $url['path'] = '/'; - $pathSplit = preg_split("/\//", $url['path'], 0, PREG_SPLIT_NO_EMPTY); + $pathSplit = preg_split("/\\//", $url['path'], 0, PREG_SPLIT_NO_EMPTY); $resource = array_pop($pathSplit); $user->parameters = $query; @@ -318,20 +227,10 @@ protected function addConnectionToUriHandler(IWebSocketConnection $user, $uri) $this->uriHandlers[$resource]->addConnection($user); $this->_connections[$user] = $resource; - $this->say("User has been added to $resource"); + $this->_logger->notice("User has been added to $resource"); } } - /** - * Output a line to stdout - * - * @param string $msg Message to output to the STDOUT - */ - public function say($msg = "") - { - echo date("Y-m-d H:i:s") . " | " . $msg . "\n"; - } - public function onConnectionEstablished(WebSocketStream $s) { $con = $s->getConnection(); @@ -354,7 +253,7 @@ public function onMessage(IWebSocketConnection $connection, IWebSocketMessage $m try { $this->dispatchMessage($connection, $msg); } catch (Exception $e) { - $this->say("Exception occurred while handling message:\r\n" . $e->getTraceAsString()); + $this->_logger->error("Exception occurred while handling message:\r\n" . $e->getTraceAsString()); } } @@ -371,7 +270,7 @@ public function onDisconnect(WebSocketStream $socket) $this->_connections->detach($socket->getConnection()); } } catch (Exception $e) { - $this->say("Exception occurred while handling message:\r\n" . $e->getTraceAsString()); + $this->_logger->err("Exception occurred while handling message:\r\n" . $e->getTraceAsString()); } @@ -384,7 +283,7 @@ public function onDisconnect(WebSocketStream $socket) } } - $this->sockets->detach($socket); + $this->_server->detachStream($socket); } protected function purgeUsers() @@ -419,19 +318,6 @@ public function onFlashXMLRequest(WebSocketConnectionFlash $connection) $connection->disconnect(); } - protected function getWriteStreams() - { - $resources = array(); - - foreach ($this->sockets as $socket) { - if ($socket->mustWrite()) - $resources[] = $socket->getResource(); - } - - return $resources; - } - - /** * * @param \Devristo\Phpws\Server\UriHandler\IWebSocketUriHandler $uri @@ -443,5 +329,34 @@ public function getUriHandler($uri) return $this->uriHandlers[$uri]; } + public function onData($data) + { + throw new \BadMethodCallException(); + } + + public function close() + { + fclose($this->getSocket()); + } + + public function mayWrite() + { + return; + } + + public function requestsWrite() + { + return false; + } + + public function getSocket() + { + return $this->master; + } + + public function isServer() + { + return true; + } } diff --git a/tests/echo_client.php b/tests/echo_client.php index bbec7c7..91ec998 100644 --- a/tests/echo_client.php +++ b/tests/echo_client.php @@ -6,10 +6,7 @@ * Time: 21:05 */ -require_once(__DIR__."/../SplClassLoader.php"); - -$loader = new SplClassLoader("Devristo\\Phpws", __DIR__."/../src"); -$loader->register(); +require_once(__DIR__."/../vendor/autoload.php"); $client = new \Devristo\Phpws\Client\WebSocket("ws://echo.websocket.org/?encoding=text"); $client->open(); diff --git a/tests/framing.test.php b/tests/framing.test.php index 3d8c628..6ecef33 100644 --- a/tests/framing.test.php +++ b/tests/framing.test.php @@ -1,9 +1,6 @@ register(); +require_once(__DIR__."/../vendor/autoload.php"); use Devristo\Phpws\Exceptions\WebSocketMessageNotFinalised; use Devristo\Phpws\Framing\WebSocketFrame; diff --git a/tests/server.test.php b/tests/server.test.php index f01eb45..e124f57 100644 --- a/tests/server.test.php +++ b/tests/server.test.php @@ -1,9 +1,6 @@ register(); +require_once(__DIR__."/../vendor/autoload.php"); use Devristo\Phpws\Client\WebSocket; use Devristo\Phpws\Framing\WebSocketFrame; @@ -25,7 +22,7 @@ function testMasking() { - $client = new WebSocket("ws://127.0.0.1:12345/echo/"); + $client = new WebSocket("wss://127.0.0.1:12345/echo/"); $client->open(); $client->sendFrame($frame); @@ -40,7 +37,7 @@ function test_echoResourceHandlerResponse() { $input = "Hello World!"; $msg = WebSocketMessage::create($input); - $client = new WebSocket("ws://127.0.0.1:12345/echo/"); + $client = new WebSocket("wss://127.0.0.1:12345/echo/"); $client->open(); $client->sendMessage($msg); @@ -56,7 +53,7 @@ function test_DoubleEchoResourceHandlerResponse() { $input2 = str_repeat("b", 1024); $msg = WebSocketMessage::create($input); - $client = new WebSocket("ws://127.0.0.1:12345/echo/"); + $client = new WebSocket("wss://127.0.0.1:12345/echo/"); $client->setTimeOut(1000); $client->open(); $client->sendMessage($msg);