/** * {@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 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'); }