コード例 #1
0
ファイル: HttpPolling.php プロジェクト: adon988/phpsocket.io
 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')));
 }
コード例 #2
0
ファイル: Handshake.php プロジェクト: adon988/phpsocket.io
 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');
 }
コード例 #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);
 }
コード例 #4
0
ファイル: SocketIO.php プロジェクト: adon988/phpsocket.io
 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);
 }
コード例 #5
0
 public function test_getDispatcher()
 {
     $this->assertTrue(EventDispatcher::getDispatcher() instanceof EventDispatcher);
 }