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