Exemplo n.º 1
0
 protected function initEvent()
 {
     $dispatcher = Event\EventDispatcher::getDispatcher();
     $dispatcher->addListener("connect", function () {
         $endpoint = $this->getRequest()->getSession()->get('endpoint');
         $this->writeChunkEnd(ProtocolBuilder::Connect($endpoint));
     }, array($this->connection->getSessionId()));
     $dispatcher->addListener("server.emit", function (Event\MessageEvent $messageEvent) {
         $message = $messageEvent->getMessage();
         if ($this->connection->isConnectionClose()) {
             $this->connection->queuePendingEmitEvent("server.emit", $message);
             return;
         }
         $endpoint = $this->getRequest()->getSession()->get('endpoint', $messageEvent->getEndpoint());
         $this->writeChunkEnd(ProtocolBuilder::Event(array('name' => $message['event'], 'args' => array($message['message'])), $endpoint));
     }, array($this->connection->getSessionId(), $this->getRequest()->getSession()->get('endpoint')));
 }
Exemplo n.º 2
0
 public static function processProtocol($data, ConnectionInterface $connection)
 {
     if (!preg_match('/^(.*?):(.*?):(.*)/i', $data, $match)) {
         return new Response('bad protocol', 404);
     }
     list($raw, $type, $id, $postRawData) = $match;
     list($endpoint, ) = explode(':', $postRawData);
     $jsonData = substr($postRawData, strlen($endpoint) + 1);
     switch ($type) {
         case 1:
             //Connect
             $connection->getRequest()->getSession()->set('endpoint', $endpoint);
             if (Event\EventDispatcher::getDispatcher()->dispatch('connect', null, $connection->getSessionId())) {
                 break;
             }
             $connection->queueEvent('connect', null, $connection->getSessionId());
             break;
         case 2:
             //Heartbeat
             break;
         case 5:
             //Event
             $eventData = json_decode($jsonData, true);
             if (!isset($eventData['name']) && !isset($eventData['args'])) {
                 return new Response('bad protocol', 402);
             }
             $messageEvent = new Event\MessageEvent();
             $messageEvent->setEndpoint($endpoint);
             $messageEvent->setMessage($eventData['args'][0]);
             $messageEvent->setConnection($connection);
             $dispatcher = Event\EventDispatcher::getDispatcher();
             $dispatcher->dispatch("client.{$eventData['name']}", $messageEvent, $endpoint);
             break;
     }
     return new Response('1');
 }
Exemplo n.º 3
0
 protected function initEvent($group)
 {
     $dispatcher = Event\EventDispatcher::getDispatcher();
     $dispatcher->addListener("socket.receive", function (Event\MessageEvent $messageEvent) {
         $message = $messageEvent->getMessage();
         $frame = $this->websocket->onMessage($message);
         if (!$frame instanceof WebSocket\Frame) {
             return;
         }
         Handshake::processProtocol($frame->getData(), $this->getConnection());
     }, $group);
     $dispatcher->addListener("server.emit", function (Event\MessageEvent $messageEvent) {
         $endpoint = $this->getRequest()->getSession()->get('endpoint', $messageEvent->getEndpoint());
         $message = $messageEvent->getMessage();
         $this->sendData(ProtocolBuilder::Event(array('name' => $message['event'], 'args' => array($message['message'])), $endpoint));
     }, $group);
 }
Exemplo n.º 4
0
 public function emit($eventName, $message)
 {
     $messageEvent = new Event\MessageEvent();
     $messageEvent->setMessage(array('event' => $eventName, 'message' => $message));
     $dispatcher = Event\EventDispatcher::getDispatcher();
     $dispatcher->dispatch("server.emit", $messageEvent);
 }
Exemplo n.º 5
0
 public function test_getDispatcher()
 {
     $this->assertTrue(EventDispatcher::getDispatcher() instanceof EventDispatcher);
 }