/** * {@inheritdoc} */ public function attach(TopicsManager $manager) { $this->pull->on('message', function ($msg) use($manager) { $data = @json_decode($msg, true); if (json_last_error() !== JSON_ERROR_NONE) { $this->logger->error(sprintf('[WS] Received invalid message %s : invalid json', $msg)); return; } if (!isset($data['topic'])) { $this->logger->error(sprintf('[WS] Received invalid message %s : no topic', $msg)); return; } $this->logger->debug(sprintf('[WS] Received message %s', $msg)); $manager->broadcast($data['topic'], json_encode($msg)); }); }
public function register(Application $app) { $app['ws.publisher.options'] = $app->share(function (Application $app) { return array_replace(['protocol' => 'tcp', 'host' => '127.0.0.1', 'port' => 13598], $app['conf']->get(['main', 'websocket-server', 'subscriber'], [])); }); $app['ws.task-manager.broadcaster'] = $app->share(function (Application $app) { return TaskManagerBroadcasterSubscriber::create($app['ws.publisher.options']); }); $app['ws.event-loop'] = $app->share(function () { return EventLoopFactory::create(); }); $app['ws.server.subscriber'] = $app->share(function (Application $app) { return new TaskManagerSubscriberPlugin($app['ws.publisher.options'], $app['ws.event-loop'], $app['ws.server.logger']); }); $app['ws.server.application'] = $app->share(function (Application $app) { return new SessionProvider(new WampServer($app['ws.server.phraseanet-server']), $app['session.storage.handler']); }); $app['ws.server.phraseanet-server'] = $app->share(function (Application $app) { return new PhraseanetWampServer($app['ws.server.topics-manager'], $app['ws.server.logger']); }); $app['ws.server.logger'] = $app->share(function (Application $app) { return $app['task-manager.logger']; }); $app['ws.server.topics-manager.directives.conf'] = $app->share(function (Application $app) { return [new Directive(TopicsManager::TOPIC_TASK_MANAGER, true, ['task-manager'])]; }); $app['ws.server.topics-manager.directives'] = $app->share(function (Application $app) { return new DirectivesManager($app['ws.server.topics-manager.directives.conf']); }); $app['ws.server.consumer-manager'] = $app->share(function (Application $app) { return new ConsumerManager(); }); $app['ws.server.topics-manager'] = $app->share(function (Application $app) { $manager = new TopicsManager($app['ws.server.topics-manager.directives'], $app['ws.server.consumer-manager']); $manager->attach($app['ws.server.subscriber']); return $manager; }); $app['ws.server.options'] = $app->share(function (Application $app) { return array_replace(['host' => 'localhost', 'port' => 9090, 'ip' => '127.0.0.1'], $app['conf']->get(['main', 'websocket-server'], [])); }); $app['ws.server'] = $app->share(function (Application $app) { $options = $app['ws.server.options']; $server = new App($options['host'], $options['port'], $options['ip'], $app['ws.event-loop']); $server->route('/websockets', $app['ws.server.application']); return $server; }); }
public function testBroadcast() { $directivesManager = $this->createDirectivesManagerMock(); $consumerManager = $this->createConsumerManagerMock(); $directivesManager->expects($this->once())->method('hasAccess')->will($this->returnValue(true)); $manager = new TopicsManager($directivesManager, $consumerManager); $conn = $this->getMock('Ratchet\\ConnectionInterface'); $conn->User = $this->getMock('Alchemy\\Phrasea\\Websocket\\Consumer\\ConsumerInterface'); $topic = $this->getMockBuilder('Ratchet\\Wamp\\Topic')->disableOriginalConstructor()->getMock(); $topic->expects($this->any())->method('getId')->will($this->returnValue('http://topic')); $topic->expects($this->once())->method('broadcast')->with('hello world !'); // should be subscribed to be unsubscribed $manager->subscribe($conn, $topic); $manager->broadcast('http://topic', 'hello world !'); $manager->broadcast('http://topic2', 'nothing'); }