PHPPM\React\Server::listen PHP Method

listen() public method

public listen ( $port, $host = '127.0.0.1' )
    public function listen($port, $host = '127.0.0.1')
    {
        $localSocket = '';
        if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
            $localSocket = 'tcp://' . $host . ':' . $port;
        } elseif (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
            // enclose IPv6 addresses in square brackets before appending port
            $localSocket = 'tcp://[' . $host . ']:' . $port;
        } elseif (preg_match('#^unix://#', $host)) {
            $localSocket = $host;
        } else {
            throw new \UnexpectedValueException('"' . $host . '" does not match to a set of supported transports. ' . 'Supported transports are: IPv4, IPv6 and unix:// .', 1433253311);
        }
        $this->master = stream_socket_server($localSocket, $errno, $errstr);
        if (false === $this->master) {
            $message = "Could not bind to {$localSocket} . Error: [{$errno}] {$errstr}";
            throw new ConnectionException($message, $errno);
        }
        stream_set_blocking($this->master, 0);
        $this->loop->addReadStream($this->master, function ($master) {
            $newSocket = stream_socket_accept($master);
            if (false === $newSocket) {
                $this->emit('error', array(new \RuntimeException('Error accepting new connection')));
                return;
            }
            $this->handleConnection($newSocket);
        });
    }

Usage Example

Example #1
0
 /**
  * Starts the main loop. Blocks.
  */
 public function run()
 {
     Debug::enable();
     //make whatever is necessary to disable all stuff that could buffer output
     ini_set('zlib.output_compression', 0);
     ini_set('output_buffering', 0);
     ini_set('implicit_flush', 1);
     ob_implicit_flush(1);
     $this->loop = \React\EventLoop\Factory::create();
     $this->controller = new React\Server($this->loop);
     $this->controller->on('connection', array($this, 'onSlaveConnection'));
     $this->controllerHost = $this->getNewControllerHost();
     $this->controller->listen(5500, $this->controllerHost);
     $this->web = new \React\Socket\Server($this->loop);
     $this->web->on('connection', array($this, 'onWeb'));
     $this->web->listen($this->port, $this->host);
     $this->tcpConnector = new \React\SocketClient\TcpConnector($this->loop);
     $pcntl = new \MKraemer\ReactPCNTL\PCNTL($this->loop);
     $pcntl->on(SIGTERM, [$this, 'shutdown']);
     $pcntl->on(SIGINT, [$this, 'shutdown']);
     $pcntl->on(SIGCHLD, [$this, 'handleSigchld']);
     $pcntl->on(SIGUSR1, [$this, 'restartWorker']);
     if ($this->isDebug()) {
         $this->loop->addPeriodicTimer(0.5, function () {
             $this->checkChangedFiles();
         });
     }
     $this->isRunning = true;
     $loopClass = (new \ReflectionClass($this->loop))->getShortName();
     $this->output->writeln("<info>Starting PHP-PM with {$this->slaveCount} workers, using {$loopClass} ...</info>");
     for ($i = 0; $i < $this->slaveCount; $i++) {
         $this->newInstance(5501 + $i);
     }
     $this->loop->run();
 }
All Usage Examples Of PHPPM\React\Server::listen