Go to file
Chris Boden 67538706ed Revert "Merge branch 'dev' of github.com:cboden/Ratchet into dev"
This reverts commit b31a65a99a, reversing
changes made to 68a618c1cd.
2011-11-08 09:39:25 -05:00
lib/Ratchet Revert "Merge branch 'dev' of github.com:cboden/Ratchet into dev" 2011-11-08 09:39:25 -05:00
tests Cleanup 2011-11-07 11:55:07 -05:00
.gitignore Fixed Socket bugs from Unit Testing 2011-09-06 14:30:14 -04:00
LICENSE Moar cleaning 2011-11-01 11:01:43 -04:00
phpunit.xml.dist Stubs, coverage, api docs 2011-09-05 08:53:21 -04:00
README.md Documentation 2011-11-07 12:06:01 -05:00

#Ratchet

A PHP 5.3 (PSR-0 compliant) application for serving and consuming sockets. Build up your application (like Lego!) through simple interfaces using the decorator pattern. Re-use your application without changing any of its code just by wrapping it in a different protocol.


##WebSockets

  • Supports the HyBi-10 and Hixie76 protocol versions (at the same time)
  • Tested on Chrome 14, Firefox 7, Safari 5, iOS 4.2

###A quick server example

<?php
namespace MyApps;
use Ratchet\SocketObserver, Ratchet\SocketInterface;
use Ratchet\Socket, Ratchet\Server, Ratchet\Protocol\WebSocket;
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;
    }

    public function onOpen(SocketInterface $conn) {
        $this->_clients->attach($conn);
    }

    public function onRecv(SocketInterface $from, $msg) {
        $stack = $this->_factory->newComposite();

        foreach ($this->_clients as $client) {
            if ($from != $client) {
                $stack->enqueue($this->_factory->newCommand('SendMessage', $client)->setMessage($msg));
            }
        }

        return $stack;
    }

    public function onClose(SocketInterface $conn) {
        $this->_clients->detach($conn);
    }
}

    // Run the server application through the WebSocket protocol
    $server = new Server(new Socket, new WebSocket(new Chat));
    $server->run('0.0.0.0', 80);