Example #1
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');
 }