Example #1
0
 /**
  * @param Client $client
  * @param $route
  * @param array $args
  * @param string $method
  * @throws \InvalidArgumentException
  */
 public function __construct(Client $client, $route, array $args, $method = 'GET')
 {
     $this->url = $client->getRouter()->route($route, $args);
     $this->method = $method;
     $this->headers = $client->getHeaders();
     $this->client = $client;
     $this->buffer = new StreamBuffer();
     $this->events = new Dispatcher();
     $this->headers['Connection'] = 'Keep-Alive';
     $this->buffer->subscribe(function ($message) {
         $message = trim($message);
         if ($message) {
             $this->events->fire(static::EVENT_DATA, [$message]);
             $data = json_decode(trim($message), true);
             if (json_last_error() === JSON_ERROR_NONE) {
                 $this->events->fire(static::EVENT_MESSAGE, [$this, $data]);
             } else {
                 $this->events->fire(static::EVENT_ERROR, [$this, new \LogicException(json_last_error_msg())]);
             }
         }
     });
     $this->connect();
 }
Example #2
0
 /**
  * @param Client $client
  * @param $route
  * @param array $args
  * @param string $method
  * @throws \InvalidArgumentException
  */
 public function __construct(Client $client, $route, array $args, $method = 'GET')
 {
     $this->url = $client->getRouter()->route($route, $args);
     $this->method = $method;
     $this->headers = $client->getHeaders();
 }