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
* 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
---
@ -17,18 +17,20 @@ Re-use your application without changing any of its code just by wrapping it in
```php
<?php
namespace Me;
namespace MyApps;
use Ratchet\SocketObserver, Ratchet\SocketInterface;
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)
*/
class Chat implements SocketObserver {
protected $_factory;
protected $_clients;
public function __construct() {
$this->_factory = new Factory;
$this->_clients = new \SplObjectStorage;
}
@ -37,13 +39,11 @@ class Chat implements SocketObserver {
}
public function onRecv(SocketInterface $from, $msg) {
$stack = new Composite;
$stack = $this->_factory->newComposite();
foreach ($this->_clients as $client) {
if ($from != $client) {
$message = new SendMessage($client);
$message->setMessage($msg);
$stack->enqueue($message);
$stack->enqueue($this->_factory->newCommand('SendMessage', $client)->setMessage($msg));
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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