Go to file
Chris Boden 7c5c5ed6ce Standardized Interfaces
Allowed null to be returned instead of NullCommand on Observers
Removed profanity
2011-11-01 11:44:28 -04:00
lib/Ratchet Standardized Interfaces 2011-11-01 11:44:28 -04:00
tests Moar cleaning 2011-11-01 11:01:43 -04: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-01 11:14:23 -04:00

#Ratchet

A PHP 5.3 (PSR-0 compliant) application for serving and consuming sockets.


##WebSocket

Ratchet (so far) includes an "application" (in development) to handle the WebSocket protocol.


###A Quick server example

<?php
    namespace Me;
    use Ratchet\SocketObserver, Ratchet\SocketInterface;
    use Ratchet\Socket, Ratchet\Server, Ratchet\Protocol\WebSocket;
    use Ratchet\SocketCollection, Ratchet\Command\SendMessage;

    /**
     * Send any incoming messages to all connected clients (except sender)
     */
    class Chat implements SocketObserver {
        protected $_clients;

        public function __construct() {
            $this->_clients = new \SplObjectStorage;
        }

        public function onOpen(SocketInterface $conn) {
            $this->_clients->attach($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 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);