Skip to content

Commit

Permalink
fix bugs with token
Browse files Browse the repository at this point in the history
  • Loading branch information
Stroia Laurentiu committed Apr 25, 2016
1 parent 5d1dca9 commit 8e19242
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*.php~
/composer.lock
/vendor/
/
/.GFS_NOTIFICATION_TOKEN
7 changes: 4 additions & 3 deletions Command/ServerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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();
}
}
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion Notification/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
namespace GFS\NotificationBundle\Notification;


use GFS\NotificationBundle\NotificationBundle;
use Guzzle\Http\QueryString;
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;

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());
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions NotificationBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@

class NotificationBundle extends Bundle
{
public static function getGfsNotificationToken(){
return __DIR__ . DIRECTORY_SEPARATOR . '.GFS_NOTIFICATION_TOKEN';
}
}
3 changes: 2 additions & 1 deletion Services/NotificationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}

Expand Down

0 comments on commit 8e19242

Please sign in to comment.