From 8e19242be8c6ecc8560e139b0c168a40c7ac4d03 Mon Sep 17 00:00:00 2001 From: Stroia Laurentiu Date: Mon, 25 Apr 2016 11:42:26 +0300 Subject: [PATCH] fix bugs with token --- .gitignore | 2 +- Command/ServerCommand.php | 7 ++++--- DependencyInjection/Configuration.php | 2 +- Notification/Notification.php | 5 ++++- NotificationBundle.php | 3 +++ Services/NotificationHelper.php | 3 ++- 6 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index db96acd..346e316 100755 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ *.php~ /composer.lock /vendor/ -/ +/.GFS_NOTIFICATION_TOKEN diff --git a/Command/ServerCommand.php b/Command/ServerCommand.php index 7946fa3..8eda01c 100644 --- a/Command/ServerCommand.php +++ b/Command/ServerCommand.php @@ -3,7 +3,7 @@ namespace GFS\NotificationBundle\Command; -use GFS\NotificationBundle\Notification\Notification; +use GFS\NotificationBundle\NotificationBundle; use Ratchet\Http\HttpServer; use Ratchet\Server\IoServer; use Ratchet\WebSocket\WsServer; @@ -21,8 +21,8 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { $port = $this->getContainer()->getParameter('gfs_notifications.config')['port']; - $notification = $this->getContainer()->get('gfs_notifications.config')['notification']; - define('GFS_NOTIFICATION_TOKEN', md5(uniqid(rand(),true))); + file_put_contents(NotificationBundle::getGfsNotificationToken(),md5(uniqid(rand(),true))); + $notification = $this->getContainer()->getParameter('gfs_notifications.config')['notification']; $server = IoServer::factory( new HttpServer( new WsServer( @@ -32,6 +32,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $port ); + $output->writeln('['.date('Y-m-d H:i:s').']Notification server has started.'); $server->run(); } } \ No newline at end of file diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index f8aef19..8b710f9 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -15,7 +15,7 @@ public function getConfigTreeBuilder(){ $rootNode->children() ->scalarNode('host')->defaultValue('localhost')->end() ->scalarNode('port')->defaultValue('8080')->end() - ->scalarNode('notification')->defaultValue('GFS\NotificationBundle\Notification')->end() + ->scalarNode('notification')->defaultValue('GFS\NotificationBundle\Notification\Notification')->end() ->end(); return $treeBuilder; diff --git a/Notification/Notification.php b/Notification/Notification.php index 00bf9e7..ad0cf60 100644 --- a/Notification/Notification.php +++ b/Notification/Notification.php @@ -3,6 +3,7 @@ namespace GFS\NotificationBundle\Notification; +use GFS\NotificationBundle\NotificationBundle; use Guzzle\Http\QueryString; use Ratchet\ConnectionInterface; use Ratchet\MessageComponentInterface; @@ -10,9 +11,11 @@ class Notification implements MessageComponentInterface{ protected $users = []; + private $GFS_NOTIFICATION_TOKEN; public function __construct(){ $this->clients = new \SplObjectStorage(); + $this->GFS_NOTIFICATION_TOKEN = file_get_contents(NotificationBundle::getGfsNotificationToken()); } /** @@ -73,7 +76,7 @@ function onMessage(ConnectionInterface $from, $msg) { $notification = json_decode($msg,true); - if($notification['token'] == GFS_NOTIFICATION_TOKEN){ + if($notification['token'] == $this->GFS_NOTIFICATION_TOKEN){ $notification = $notification['notification']; if(!empty($this->users[$notification['userId']])){ foreach ($this->users[$notification['userId']] as $client) { diff --git a/NotificationBundle.php b/NotificationBundle.php index 95f09f4..88550c3 100644 --- a/NotificationBundle.php +++ b/NotificationBundle.php @@ -6,4 +6,7 @@ class NotificationBundle extends Bundle { + public static function getGfsNotificationToken(){ + return __DIR__ . DIRECTORY_SEPARATOR . '.GFS_NOTIFICATION_TOKEN'; + } } diff --git a/Services/NotificationHelper.php b/Services/NotificationHelper.php index 8ba4dc2..6cde088 100644 --- a/Services/NotificationHelper.php +++ b/Services/NotificationHelper.php @@ -5,6 +5,7 @@ use GFS\NotificationBundle\Entity\Notification; use GFS\NotificationBundle\Notification\SocketIO; +use GFS\NotificationBundle\NotificationBundle; use Symfony\Component\DependencyInjection\Container; class NotificationHelper { @@ -28,7 +29,7 @@ public function sendNotification(Notification $notification){ $socketio = new SocketIO(); $host = $this->container->getParameter('gfs_notifications.config')['host']; $port = $this->container->getParameter('gfs_notifications.config')['port']; - if($socketio->send($host,$port,json_encode([ 'notification' => $notification, 'token' => GFS_NOTIFICATION_TOKEN]),"ws://$host:$port/")){ + if($socketio->send($host,$port,json_encode([ 'notification' => $notification, 'token' => file_get_contents(NotificationBundle::getGfsNotificationToken())]),"ws://$host:$port/")){ return true; }