Added PHP syntax to README
This commit is contained in:
Chris Boden 2011-11-01 14:34:29 -04:00
parent 1fe4b051be
commit 6a5c708775

View File

@ -15,44 +15,46 @@ Re-use your application without changing any of its code just by wrapping it in
###A Quick server example ###A Quick server example
<?php ```php
namespace Me; <?php
use Ratchet\SocketObserver, Ratchet\SocketInterface; namespace Me;
use Ratchet\Socket, Ratchet\Server, Ratchet\Protocol\WebSocket; use Ratchet\SocketObserver, Ratchet\SocketInterface;
use Ratchet\SocketCollection, Ratchet\Command\SendMessage; use Ratchet\Socket, Ratchet\Server, Ratchet\Protocol\WebSocket;
use Ratchet\SocketCollection, Ratchet\Command\SendMessage;
/** /**
* 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 $_clients; protected $_clients;
public function __construct() { public function __construct() {
$this->_clients = new \SplObjectStorage; $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 public function onOpen(SocketInterface $conn) {
$server = new Server(new Socket, new WebSocket(new Chat)); $this->_clients->attach($conn);
$server->run('0.0.0.0', 80); }
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);
```