Documentation

Updated the simple user documentation in README
This commit is contained in:
Chris Boden 2011-11-01 11:14:23 -04:00
parent f9d609074c
commit 68e718cc54

View File

@ -14,31 +14,42 @@ Ratchet (so far) includes an "application" (in development) to handle the WebSoc
<?php <?php
namespace Me; namespace Me;
use Ratchet\Socket; use Ratchet\SocketObserver, Ratchet\SocketInterface;
use Ratchet\SocketInterface as Sock; use Ratchet\Socket, Ratchet\Server, Ratchet\Protocol\WebSocket;
use Ratchet\Server; use Ratchet\SocketCollection, Ratchet\Command\SendMessage;
use Ratchet\Protocol\WebSocket;
class MyApp implements \Ratchet\ReceiverInterface { /**
protected $_server; * Send any incoming messages to all connected clients (except sender)
*/
class Chat implements SocketObserver {
protected $_clients;
public function getName() { public function __construct() {
return 'my_app'; $this->_clients = new \SplObjectStorage;
} }
public function setUp(Server $server) { public function onOpen(SocketInterface $conn) {
$this->_server = $server; $this->_clients->attach($conn);
} }
public function onOpen(Sock $conn) { public function onRecv(SocketInterface $from, $msg) {
$stack = new SocketCollection;
foreach ($this->_clients as $client) {
if ($from != $client) {
$stack->enqueue($client);
}
}
$command = new SendMessage($stack);
$command->setMessage($msg);
return $command;
} }
public function onRecv(Sock $from, $msg) { public function onClose(SocketInterface $conn) {
} $this->_clients->detach($conn);
public function onClose(Sock $conn) {
} }
} }
$server = new Server(new Socket, new WebSocket(new MyApp)); // Run the server application through the WebSocket protocol
$server = new Server(new Socket, new WebSocket(new Chat));
$server->run('0.0.0.0', 80); $server->run('0.0.0.0', 80);