freenet-router |
Subversion Repositories: |
Compare with Previous - Blame - Download
<?php
namespace Phem\Libraries\MessageBus;
use Exception;
use Phem\Core\Collection;
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
/**
* @author Jakub PetrĹžĂlka <petrzilka@czweb.net>
*/
class MessageBus implements MessageComponentInterface
{
protected $clients;
public function __construct()
{
$this->clients = new Collection();
}
public function onOpen(ConnectionInterface $conn)
{
// Store the new connection to send messages to later
$this->clients->put($conn->resourceId, $conn);
echo "New connection! ({$conn->resourceId})\n";
$client = $this->clients->get($conn->resourceId);
//$client->send('{"acceptedConnection":"'.$conn->resourceId.'"}');
return true;
}
public function onNotify($msg)
{
foreach ($this->clients as $client)
{
$client->send($msg);
}
}
public function onMessage(ConnectionInterface $from, $msg)
{
$msgobj = json_decode($msg);
foreach ($this->clients as $client)
{
$client->send($msg);
}
}
public function onClose(ConnectionInterface $conn)
{
\Phem\Environment\Application::onConnectionClose($conn->resourceId);
// The connection is closed, remove it, as we can no longer send it messages
$this->clients->removeKey($conn->resourceId);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, Exception $e)
{
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}