Пример #1
0
 private function startConnection()
 {
     $loop = \EventLoop\getLoop();
     $dnsResolverFactory = new Factory();
     $dnsResolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);
     $factory = new \React\HttpClient\Factory();
     $client = $factory->create($loop, $dnsResolver);
     $cNegotiator = new ClientNegotiator($this->url, $this->subProtocols);
     $headers = $cNegotiator->getRequest()->getHeaders();
     $flatHeaders = [];
     foreach ($headers as $k => $v) {
         $flatHeaders[$k] = $v[0];
     }
     $request = $client->request("GET", $this->url, $flatHeaders, '1.1');
     $request->on('response', function (Response $response, Request $request) use($cNegotiator) {
         if ($response->getCode() !== 101) {
             throw new \Exception("Unexpected response code " . $response->getCode());
         }
         // TODO: Should validate response
         //$cNegotiator->validateResponse($response);
         $subprotoHeader = "";
         $psr7Response = new \GuzzleHttp\Psr7\Response($response->getCode(), $response->getHeaders(), null, $response->getVersion());
         if (count($psr7Response->getHeader('Sec-WebSocket-Protocol')) == 1) {
             $subprotoHeader = $psr7Response->getHeader('Sec-WebSocket-Protocol')[0];
         }
         parent::onNext(new MessageSubject(new AnonymousObservable(function (ObserverInterface $observer) use($response) {
             $response->on('data', function ($data) use($observer) {
                 $observer->onNext($data);
             });
             $response->on('error', function ($e) use($observer) {
                 $observer->onError($e);
             });
             $response->on('close', function () use($observer) {
                 $observer->onCompleted();
             });
             $response->on('end', function () use($observer) {
                 $observer->onCompleted();
                 // complete the parent observer - we only do 1 connection
                 parent::onCompleted();
             });
             return new CallbackDisposable(function () use($response) {
                 // commented this out because disposal was causing the other
                 // end (the request) to close also - which causes the pending messages
                 // to get tossed
                 //$response->close();
             });
         }), new CallbackObserver(function ($x) use($request) {
             $request->write($x);
         }, function ($e) use($request) {
             $request->close();
         }, function () use($request) {
             $request->end();
         }), true, $this->useMessageObject, $subprotoHeader, $cNegotiator->getRequest(), $psr7Response));
     });
     $request->writeHead();
 }
Пример #2
0
 public function __construct($method, $url, $body = null, array $headers = [], $protocolVersion = '1.0', $bufferResults = true, $includeResponse = false)
 {
     $this->method = $method;
     $this->url = $url;
     $this->body = $body;
     $this->headers = $headers;
     $this->protocolVersion = $protocolVersion;
     $this->bufferResults = $bufferResults;
     $this->includeResponse = $includeResponse;
     $loop = \EventLoop\getLoop();
     $dnsResolverFactory = new Factory();
     $dnsResolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);
     $factory = new \React\HttpClient\Factory();
     $this->client = $factory->create($loop, $dnsResolver);
 }
Пример #3
0
 /**
  * Generates request for HTTP Client (API), and add event listners
  * 
  * @param string $method 
  * @param string $url
  * @param array $headers
  * 
  * @return React\HttpClient\Request
  */
 protected function createClientRequest($method, $url, $headers)
 {
     $request = $this->client->request($method, $url, $headers);
     foreach ($this->listeners as $event => $listeners) {
         $request->on($event, function () use($event) {
             $this->emit($event, func_get_args());
         });
     }
     return $request;
 }
Пример #4
0
 protected function makeHttpRequest($method, $url, array $headers = [], $protocolVersion = '1.0', $body)
 {
     $dnsResolverFactory = new Factory();
     $dnsResolver = $dnsResolverFactory->createCached("127.0.0.1", \EventLoop\getLoop());
     $httpFactory = new \React\HttpClient\Factory();
     $httpClient = $httpFactory->create(\EventLoop\getLoop(), $dnsResolver);
     $deferred = new Deferred();
     $request = $httpClient->request($method, $url, $headers, $protocolVersion);
     $request->writeHead();
     $request->write($body);
     $request->on('response', function ($response) use($deferred) {
         $responseBody = "";
         $response->on('data', function ($data, $response) use(&$responseBody) {
             $responseBody .= $data;
         });
         $response->on('end', function () use(&$responseBody, $deferred, $response) {
             $deferred->resolve([$response, $responseBody]);
         });
     });
     $request->end();
     return $deferred->promise();
 }
Пример #5
0
<?php

function call($client, $url)
{
    $request = $client->request('GET', $url);
    $request->on('response', function ($response) {
        $response->on('data', function ($data, $response) {
            echo $data;
        });
        sleep(1);
    });
    $request->end();
}
require 'vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$dnsResolverFactory = new React\Dns\Resolver\Factory();
$dnsResolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);
$factory = new React\HttpClient\Factory();
$client = $factory->create($loop, $dnsResolver);
call($client, 'http://blog.wyrihaximus.net/robots.txt');
call($client, 'http://reactphp.org/');
$loop->run();
Пример #6
0
<?php

/**
 * Common bootstrap for all examples
 */
if (file_exists($path = __DIR__ . '/../vendor/autoload.php')) {
    require_once $path;
} elseif (file_exists($path = __DIR__ . '/../../../../vendor/autoload.php')) {
    require_once $path;
}
// Init react
$eventLoop = React\EventLoop\Factory::create();
$dnsFactory = new React\Dns\Resolver\Factory();
$dnsResolver = $dnsFactory->createCached("8.8.8.8", $eventLoop);
$httpClientFactory = new React\HttpClient\Factory();
$httpClient = $httpClientFactory->create($eventLoop, $dnsResolver);
// Register flow() function
Flow\Flow::register(new Flow\Schedulers\HorizontalScheduler($eventLoop));
Пример #7
0
 /**
  * @param $method
  * @param null $params
  * @return \React\Promise\Promise
  */
 private function request($method, $params = null)
 {
     $deferred = new Deferred();
     $loop = $this->getLoop();
     $dnsResolverFactory = new \React\Dns\Resolver\Factory();
     $dnsResolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);
     $factory = new \React\HttpClient\Factory();
     $client = $factory->create($loop, $dnsResolver);
     $request = $client->request('GET', $this->slackURL . $method . '?token=' . $this->token . '&' . $params);
     $buffer = '';
     $request->on('response', function ($response) use(&$buffer, $deferred) {
         $response->on('data', function ($data) use(&$buffer) {
             $buffer .= $data;
         });
         $response->on('error', function ($error) use($deferred) {
             $deferred->reject($error);
         });
     });
     $request->on('end', function () use(&$buffer, $deferred) {
         $deferred->resolve(json_decode($buffer));
     });
     $request->on('error', function ($error) use($deferred) {
         $deferred->reject($error);
     });
     $request->end();
     return $deferred->promise();
 }