amphp / websocket-client

Async WebSocket client for PHP based on Amp.
https://amphp.org/websocket-client
MIT License
148 stars 18 forks source link

Getting error after making a subsequent request in the while condition #39

Closed thecyrilcril closed 2 years ago

thecyrilcril commented 2 years ago
PHP Notice:  Undefined offset: 1 in C:\wamp64\www\etradexchange-api\vendor\amphp\websocket\src\Rfc6455Client.php on line 274

code

use Amp\Delayed;
use Amp\Websocket;
use Amp\Websocket\Client;

class SomeService implements \App\Interfaces\SomeInterface
{
  private $connection;
  private $transaction;
  public $error_message;
  public $user_message;

  protected static $instance = null;

  public static function run($transaction)
  {
      if ( is_null ( self::$instance ) ) {
          self::$instance = new self($transaction);
      }

      return self::$instance;
  }

  public function __construct($transaction) {

    $this->transaction = $transaction;
  }

  public function authorize(callable $callback, $must_authorize = true) {
      Amp\Loop::run(function () use($callback, $must_authorize) {
        try {

            $connection = yield Client\connect(config('~.url') . '?app_id=' . config('~.app_id'));

          if ($must_authorize) {
            $connection->send(json_encode([
              'authorize' => config('~.btc.token')
            ]));

          }

        while ($message = yield $connection->receive()) {

          call_user_func($callback, $connection);

          $payload = yield $message->buffer();

          $response = json_decode($payload, false);

          if (isset($response->error)) {

            $connection->close();
            throw new \App\Exceptions\SomeException($response->error->message);
            break;

          }

          ...

          if ($response->msg_type === 'exchange_rates') {
              $amount = $response->exchange_rates->rate * $this->transaction->transactable->amount_in_usd;

              call_user_func($this->transfer($amount), $connection); // culprit call that causes the error
          }

          if ($response->msg_type === 'paymentagent_transfer') {
            $connection->close();
            dd(response);

          }

        }
        } catch(Amp\Websocket\Client\ConnectionException $exception) {
          $error_json = yield $exception->getResponse()->getBody()->buffer();
          $error_object = json_decode($error_json, false);
          throw new \App\Exceptions\SomeException($error_object->error);
        }
    });

  }

  public function transfer() {
    return $this->authorize(function($connection) use($amount){
          $connection->send(json_encode([
            'transfer' => 1,
            'amount' => $amount,
            'currency' => '',
            'transfer_to' => $this->transaction->account_id,
            'description' => '',
          ]));

        }, true);

  }

  public function getResult() {
    $temp = $this->user_message;
    $this->user_message = null;
    return $temp . "\r\n";

  }

  public function getExchangeRates() {
      return $this->authorize(function($connection) {
        $connection->send(json_encode([
          'exchange_rates' => 1,
          'base_currency' => 'USD',
        ]));
    });
  }

}
$t = Transaction::with('transactable')->find(7);  
$h = SomeService::run($t);

$h->getExchangeRates();

echo $h->getResult();