Ejemplo n.º 1
0
 /**
  * Start the server
  */
 public function run(DOG_IRCWS $ircws)
 {
     error_reporting(E_ALL);
     set_time_limit(0);
     ob_implicit_flush();
     $err = $errno = 0;
     $port = parse_url($this->_url, PHP_URL_PORT);
     $this->FLASH_POLICY_FILE = str_replace('to-ports="*', 'to-ports="' . $port, $this->FLASH_POLICY_FILE);
     if (false === ($this->master = @stream_socket_server($this->_url, $errno, $err, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $this->_context))) {
         return false;
     }
     $this->say("PHP WebSocket Server");
     $this->say("========================================");
     $this->say("Server Started : " . date('Y-m-d H:i:s'));
     $this->say("Listening on   : " . $this->_url);
     $this->say("========================================");
     if ($this->master == false) {
         $this->say("Error: {$err}");
         return false;
     }
     $this->sockets->attach(new WebSocketSocket($this, $this->master));
     $ircws->connected = true;
     while (true) {
         // 			clearstatcache();
         // Garbage Collection (PHP >= 5.3)
         // 			if (function_exists('gc_collect_cycles')) {
         // 				gc_collect_cycles();
         // 			}
         //$this->debug("Blocking on socket_select()");
         $this->populateFromQueue();
         // Retreive sockets which are 'Changed'
         $changed = $this->getResources();
         $write = $this->getWriteStreams();
         $except = null;
         if (@stream_select($changed, $write, $except, 0, 500000) === false) {
             $this->say("Select failed!");
             break;
         }
         //$this->debug("Socket selected");
         foreach ($changed as $resource) {
             if ($resource == $this->master) {
                 $this->acceptSocket();
             } else {
                 $buffer = WebSocketFunctions::readWholeBuffer($resource);
                 $socket = $this->getSocketByResource($resource);
                 // If read returns false, close the stream and continue with the next socket
                 if ($buffer === false) {
                     $socket->close();
                     // Skip to next stream
                     continue;
                 }
                 $bytes = strlen($buffer);
                 if ($bytes === 0) {
                     $socket->close();
                 } else {
                     if ($socket != null) {
                         $socket->onData($buffer);
                     }
                 }
             }
         }
         if (is_array($write)) {
             foreach ($write as $s) {
                 $o = $this->getSocketByResource($s);
                 if ($o != null) {
                     $o->mayWrite();
                 }
             }
         }
         //$this->debug('Number of users connected: '.count($this->getConnections()));
         $this->purgeUsers();
     }
 }
Ejemplo n.º 2
0
 /**
  * @return WebSocketFrame
  */
 public function readFrame()
 {
     $buffer = WebSocketFunctions::readWholeBuffer($this->socket);
     $this->_frames = array_merge($this->_frames, $this->_connection->readFrame($buffer));
     return array_shift($this->_frames);
 }