Example #1
0
 /**
  * {@inheritdoc}
  */
 public function write($data = null)
 {
     $req = new UDPRequest($data, [], true);
     return $this->dns->resolve($this->host)->flatMap(function ($ip) {
         return $this->connector->connect($ip, $this->port);
     })->flatMap(function (ConnectorEvent $event) use($req) {
         /** @var Datagram $stream */
         $stream = $event->getStream();
         $stream->subscribe($req);
         $stream->subscribeCallback(null, [$stream, 'close'], [$stream, 'close'], new EventLoopScheduler(EventLoop::getLoop()));
         $stream->write($req->data, null, false);
         return $req;
     });
 }
Example #2
0
 /**
  * @param Request $request
  * @param array $opts
  * @return Observable\AnonymousObservable
  */
 public function requestRaw(Request $request, array $opts = [])
 {
     // To retry properly this observable will be retried
     return Observable::create(function (ObserverInterface $observer) use($request, $opts) {
         $scheme = $request->getUri()->getScheme();
         if (!($port = $request->getUri()->getPort())) {
             $port = $request->getUri()->getScheme() === 'http' ? 80 : 443;
         }
         $streamed = Arrays::get($opts, 'stream', false);
         $req = new HttpRequest($request, $streamed);
         $this->dns->resolve($request->getUri()->getHost())->flatMap(function ($ip) use($scheme, $opts, $port) {
             return $this->getConnector($scheme, $opts)->connect($ip, $port)->map(function (Event $e) {
                 if ($e instanceof ConnectorEvent) {
                     $stream = $e->data;
                     $bufferedStream = new BufferedStream($stream->getSocket(), $stream->getLoop());
                     return new ConnectorEvent($e->name, $bufferedStream, $e->labels, $e->getPriority());
                 }
                 return $e;
             });
         })->subscribe($req);
         $req->subscribe($observer);
     });
 }