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
<?php
namespace Me;
use Ratchet\SocketObserver, Ratchet\SocketInterface;
use Ratchet\Socket, Ratchet\Server, Ratchet\Protocol\WebSocket;
use Ratchet\SocketCollection, Ratchet\Command\SendMessage;
```php
<?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;
/**
* 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);
}
public function __construct() {
$this->_clients = new \SplObjectStorage;
}
// Run the server application through the WebSocket protocol
$server = new Server(new Socket, new WebSocket(new Chat));
$server->run('0.0.0.0', 80);
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);
```