2019-07-16 12:10:32 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Cadoles\WebsocketBundle\Topic;
|
|
|
|
|
|
|
|
use Gos\Bundle\WebSocketBundle\Topic\TopicInterface;
|
|
|
|
use Ratchet\ConnectionInterface;
|
|
|
|
use Ratchet\Wamp\Topic;
|
|
|
|
use Gos\Bundle\WebSocketBundle\Router\WampRequest;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
use Gos\Bundle\WebSocketBundle\Client\ClientManipulatorInterface;
|
|
|
|
|
|
|
|
use Cadoles\WebsocketBundle\Entity\Message;
|
|
|
|
|
|
|
|
class WebsocketTopic implements TopicInterface
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $em;
|
|
|
|
protected $container;
|
|
|
|
protected $clientManipulator;
|
|
|
|
|
|
|
|
public function __construct(ClientManipulatorInterface $clientManipulator, EntityManager $em, ContainerInterface $container) {
|
|
|
|
$this->clientManipulator = $clientManipulator;
|
|
|
|
$this->em = $em;
|
|
|
|
$this->container = $container;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
2019-07-17 11:50:37 +02:00
|
|
|
{
|
2019-07-16 12:10:32 +02:00
|
|
|
//this will broadcast the message to ALL subscribers of this topic.
|
2019-07-17 11:50:37 +02:00
|
|
|
$topic->broadcast(['log' => $connection->resourceId." has joined ".$topic->getId()]);
|
|
|
|
|
2019-07-16 12:10:32 +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)
|
|
|
|
{
|
|
|
|
//this will broadcast the message to ALL subscribers of this topic.
|
|
|
|
//$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
|
|
|
|
*/
|
|
|
|
public function onPublish(ConnectionInterface $connection, Topic $topic, WampRequest $request, $event, array $exclude, array $eligible)
|
|
|
|
{
|
|
|
|
$userkey=$connection->userkey;
|
|
|
|
$groupid=$request->getAttributes()->get('group');
|
|
|
|
|
|
|
|
$group=$this->em->getRepository("CadolesCoreBundle:Group")->find($groupid);
|
|
|
|
if(!$group) {
|
2019-07-17 11:50:37 +02:00
|
|
|
$topic->broadcast(['log' => 'group NOT FIND']);
|
2019-07-16 12:10:32 +02:00
|
|
|
}
|
|
|
|
|
2019-07-17 11:50:37 +02:00
|
|
|
$usergroup=$this->em->getRepository("CadolesCoreBundle:UserGroup")->findOneBy(['keyvalue'=> $userkey]);
|
|
|
|
if(!$usergroup) {
|
2019-07-17 14:40:12 +02:00
|
|
|
$topic->broadcast(['log' => 'user NOT IN GROUP = '.$userkey]);
|
2019-07-17 11:50:37 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$user=$usergroup->getUser();
|
2019-07-16 12:10:32 +02:00
|
|
|
}
|
|
|
|
|
2019-07-17 11:50:37 +02:00
|
|
|
if($group&&$usergroup&&$user) {
|
2019-07-16 12:10:32 +02:00
|
|
|
$message=new Message();
|
|
|
|
$message->setTopic($event);
|
|
|
|
$message->setUser($user);
|
|
|
|
$message->setGroup($group);
|
|
|
|
$this->em->persist($message);
|
|
|
|
$this->em->flush();
|
|
|
|
|
|
|
|
$return["lastname"]=$user->getLastname()." ".$user->getFirstname();
|
|
|
|
$return["avatar"]=$user->getAvatar();
|
|
|
|
$return["submitdate"]=$message->getSubmitdate();
|
|
|
|
$return["message"]=$event;
|
|
|
|
|
|
|
|
//this will broadcast the message to ALL subscribers of this topic.
|
|
|
|
$topic->broadcast([
|
|
|
|
'msg' => $return,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like RPC is will use to prefix the channel
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'websocket.topic';
|
|
|
|
}
|
|
|
|
}
|