2016-10-18 15:58:53 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Mine\SeekerBundle\Topic;
|
|
|
|
|
|
2016-10-19 13:27:55 +02:00
|
|
|
use Gos\Bundle\WebSocketBundle\Client\ClientManipulatorInterface;
|
2016-10-18 15:58:53 +02:00
|
|
|
use Gos\Bundle\WebSocketBundle\Topic\TopicInterface;
|
|
|
|
|
use Gos\Bundle\WebSocketBundle\Router\WampRequest;
|
|
|
|
|
use Ratchet\ConnectionInterface;
|
|
|
|
|
use Ratchet\Wamp\Topic;
|
|
|
|
|
|
|
|
|
|
class AcmeTopic implements TopicInterface
|
|
|
|
|
{
|
2016-10-19 13:27:55 +02:00
|
|
|
/** @var ClientManipulatorInterface */
|
|
|
|
|
protected $clientManipulator;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* AcmeTopic constructor.
|
|
|
|
|
*
|
|
|
|
|
* @param $clientManipulator ClientManipulatorInterface
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(ClientManipulatorInterface $clientManipulator)
|
|
|
|
|
{
|
|
|
|
|
$this->clientManipulator = $clientManipulator;
|
|
|
|
|
}
|
2016-10-18 15:58:53 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This will receive any Subscription requests for this topic.
|
|
|
|
|
*
|
|
|
|
|
* @param ConnectionInterface $connection
|
|
|
|
|
* @param Topic $topic
|
|
|
|
|
* @param WampRequest $request
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function onSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request)
|
|
|
|
|
{
|
2016-10-19 13:27:55 +02:00
|
|
|
/** this will broadcast the message to ALL subscribers of this topic. */
|
|
|
|
|
$user = $this->clientManipulator->getClient($connection);
|
|
|
|
|
dump($user);
|
2016-10-18 15:58:53 +02:00
|
|
|
$topic->broadcast([
|
|
|
|
|
'msg' => $connection->resourceId . " has joined " . $topic->getId(),
|
2016-10-19 13:27:55 +02:00
|
|
|
'user' => $this->getName()
|
2016-10-18 15:58:53 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This will receive any UnSubscription requests for this topic.
|
|
|
|
|
*
|
|
|
|
|
* @param ConnectionInterface $connection
|
|
|
|
|
* @param Topic $topic
|
|
|
|
|
* @param WampRequest $request
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function onUnSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request)
|
|
|
|
|
{
|
2016-10-19 13:27:55 +02:00
|
|
|
/** this will broadcast the message to ALL subscribers of this topic. */
|
|
|
|
|
$user = $this->clientManipulator->getClient($connection);
|
|
|
|
|
dump($user);
|
2016-10-18 15:58:53 +02:00
|
|
|
$topic->broadcast(['msg' => $connection->resourceId . " has left " . $topic->getId()]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This will receive any Publish requests for this topic.
|
|
|
|
|
*
|
|
|
|
|
* @param ConnectionInterface $connection
|
|
|
|
|
* @param Topic $topic
|
|
|
|
|
* @param WampRequest $request
|
|
|
|
|
* @param $event
|
|
|
|
|
* @param array $exclude
|
|
|
|
|
* @param array $eligible
|
|
|
|
|
* @return mixed|void
|
|
|
|
|
* @internal param Topic $Topic
|
|
|
|
|
* @internal param array $eligibles
|
|
|
|
|
*/
|
|
|
|
|
public function onPublish(ConnectionInterface $connection, Topic $topic, WampRequest $request, $event, array $exclude, array $eligible)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
$topic->getId() will contain the FULL requested uri, so you can proceed based on that
|
|
|
|
|
if ($topic->getId() == "acme/channel/shout")
|
|
|
|
|
//shout something to all subs.
|
|
|
|
|
*/
|
|
|
|
|
$topic->broadcast([
|
|
|
|
|
'msg' => $event
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Like RPC is will use to prefix the channel
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getName()
|
|
|
|
|
{
|
|
|
|
|
return 'acme.topic';
|
|
|
|
|
}
|
|
|
|
|
}
|