Documentation

This commit is contained in:
Chris Boden 2011-11-07 12:06:01 -05:00
parent 60a8a04e40
commit 66e656ec63
7 changed files with 30 additions and 9 deletions

View File

@ -8,7 +8,7 @@ Re-use your application without changing any of its code just by wrapping it in
##WebSockets ##WebSockets
* Supports the HyBi-10 and Hixie76 protocol versions * Supports the HyBi-10 and Hixie76 protocol versions (at the same time)
* Tested on Chrome 14, Firefox 7, Safari 5, iOS 4.2 * Tested on Chrome 14, Firefox 7, Safari 5, iOS 4.2
--- ---
@ -17,18 +17,20 @@ Re-use your application without changing any of its code just by wrapping it in
```php ```php
<?php <?php
namespace Me; namespace MyApps;
use Ratchet\SocketObserver, Ratchet\SocketInterface; use Ratchet\SocketObserver, Ratchet\SocketInterface;
use Ratchet\Socket, Ratchet\Server, Ratchet\Protocol\WebSocket; use Ratchet\Socket, Ratchet\Server, Ratchet\Protocol\WebSocket;
use Ratchet\Command\Composite, Ratchet\Command\SendMessage; use Ratchet\Command\Factory;
/** /**
* Send any incoming messages to all connected clients (except sender) * Send any incoming messages to all connected clients (except sender)
*/ */
class Chat implements SocketObserver { class Chat implements SocketObserver {
protected $_factory;
protected $_clients; protected $_clients;
public function __construct() { public function __construct() {
$this->_factory = new Factory;
$this->_clients = new \SplObjectStorage; $this->_clients = new \SplObjectStorage;
} }
@ -37,13 +39,11 @@ class Chat implements SocketObserver {
} }
public function onRecv(SocketInterface $from, $msg) { public function onRecv(SocketInterface $from, $msg) {
$stack = new Composite; $stack = $this->_factory->newComposite();
foreach ($this->_clients as $client) { foreach ($this->_clients as $client) {
if ($from != $client) { if ($from != $client) {
$message = new SendMessage($client); $stack->enqueue($this->_factory->newCommand('SendMessage', $client)->setMessage($msg));
$message->setMessage($msg);
$stack->enqueue($message);
} }
} }

View File

@ -24,6 +24,7 @@ class SendMessage implements CommandInterface {
/** /**
* The message to send to the socket(s) * The message to send to the socket(s)
* @param string * @param string
* @return SendMessage Fluid interface
*/ */
public function setMessage($msg) { public function setMessage($msg) {
$this->_message = (string)$msg; $this->_message = (string)$msg;

View File

@ -9,6 +9,7 @@ use Ratchet\SocketInterface;
interface CommandInterface { interface CommandInterface {
/** /**
* Pass the Sockets to execute the command on * Pass the Sockets to execute the command on
* @param Ratchet\SocketInterface
*/ */
function __construct(SocketInterface $socket); function __construct(SocketInterface $socket);

View File

@ -9,10 +9,17 @@ class Factory {
$this->addActionPath(__NAMESPACE__ . '\\Action'); $this->addActionPath(__NAMESPACE__ . '\\Action');
} }
/**
* Add a new namespace of which CommandInterfaces reside under to autoload with $this->newCommand()
* @param string
*/
public function addActionPath($namespace) { public function addActionPath($namespace) {
$this->_paths[] = $this->slashIt($namespace); $this->_paths[] = $this->slashIt($namespace);
} }
/**
* @return Composite
*/
public function newComposite() { public function newComposite() {
return new Composite; return new Composite;
} }
@ -38,6 +45,10 @@ class Factory {
return new $cmd($conn); return new $cmd($conn);
} }
/**
* @param string
* @return string
*/
protected function slashIt($ns) { protected function slashIt($ns) {
return (substr($ns, -1) == '\\' ? $ns : $ns . '\\'); return (substr($ns, -1) == '\\' ? $ns : $ns . '\\');
} }

View File

@ -1,7 +1,6 @@
<?php <?php
namespace Ratchet\Protocol; namespace Ratchet\Protocol;
use Ratchet\Protocol\WebSocket\Client; use Ratchet\Protocol\WebSocket\Client;
use Ratchet\Protocol\WebSocket\Version;
use Ratchet\Protocol\WebSocket\VersionInterface; use Ratchet\Protocol\WebSocket\VersionInterface;
use Ratchet\SocketInterface; use Ratchet\SocketInterface;
use Ratchet\SocketObserver; use Ratchet\SocketObserver;
@ -19,15 +18,20 @@ use Ratchet\Protocol\WebSocket\Util\HTTP;
*/ */
class WebSocket implements ProtocolInterface { class WebSocket implements ProtocolInterface {
/** /**
* Lookup for connected clients
* @type SplObjectStorage * @type SplObjectStorage
*/ */
protected $_clients; protected $_clients;
/** /**
* Decorated application
* @type Ratchet\SocketObserver * @type Ratchet\SocketObserver
*/ */
protected $_app; protected $_app;
/**
* @internal
*/
protected $_versions = array( protected $_versions = array(
'HyBi10' => null 'HyBi10' => null
, 'Hixie76' => null , 'Hixie76' => null

View File

@ -1,6 +1,9 @@
<?php <?php
namespace Ratchet\Protocol\WebSocket\Util; namespace Ratchet\Protocol\WebSocket\Util;
/**
* A helper class for handling HTTP requests
*/
class HTTP { class HTTP {
/** /**
* @param string * @param string

View File

@ -8,6 +8,7 @@ namespace Ratchet\Protocol\WebSocket\Version;
*/ */
class Hixie76 implements VersionInterface { class Hixie76 implements VersionInterface {
/** /**
* @param string
* @return string * @return string
*/ */
public function handshake($message) { public function handshake($message) {