2018-02-05 17:59:48 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Ratchet\ConnectionInterface;
|
|
|
|
use Ratchet\MessageComponentInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* class Server.
|
|
|
|
*
|
|
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
|
|
*/
|
|
|
|
class Server implements MessageComponentInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \SplObjectStorage
|
|
|
|
*/
|
|
|
|
protected $clients;
|
|
|
|
|
|
|
|
/**
|
2018-02-05 18:50:29 +01:00
|
|
|
* @var array
|
2018-02-05 17:59:48 +01:00
|
|
|
*/
|
2018-02-05 18:50:29 +01:00
|
|
|
protected $messageHandlers = [];
|
2018-02-05 17:59:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->clients = new \SplObjectStorage();
|
|
|
|
}
|
|
|
|
|
2018-02-05 18:50:29 +01:00
|
|
|
/**
|
|
|
|
* Add a message handle.
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param callable $callable
|
|
|
|
*
|
|
|
|
* @return Server;
|
|
|
|
*/
|
|
|
|
public function addMessageHandler($type, callable $callable)
|
|
|
|
{
|
|
|
|
if (!isset($this->messageHandlers[$type])) {
|
|
|
|
$this->messageHandlers[$type] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->messageHandlers[$type][] = $callable;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-02-05 17:59:48 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function onOpen(ConnectionInterface $conn)
|
|
|
|
{
|
|
|
|
$this->clients->attach($conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function onMessage(ConnectionInterface $from, $msg)
|
|
|
|
{
|
|
|
|
$data = json_decode($msg, true);
|
|
|
|
|
|
|
|
if ($data === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = $data['type'] ?? null;
|
|
|
|
|
2018-02-05 18:50:29 +01:00
|
|
|
if ($type === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$handlers = $this->messageHandlers[$type] ?? [];
|
|
|
|
|
|
|
|
foreach ($handlers as $handler) {
|
|
|
|
$handler($from, $data);
|
2018-02-05 17:59:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function onClose(ConnectionInterface $conn)
|
|
|
|
{
|
|
|
|
$this->clients->detach($conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function onError(ConnectionInterface $conn, \Exception $e)
|
|
|
|
{
|
|
|
|
$conn->close();
|
|
|
|
}
|
|
|
|
}
|