2

I'm using reactphp to create a client for an api server. But i have a problem, when my connection close, whatever the reason, i can't reconnect automatically.

It doesn't work:

$this->loop = \React\EventLoop\Factory::create();
$host = config('app.reactphp_receiver_host');
$port = config('app.reactphp_receiver_port');

$this->connector = new \React\Socket\Connector($this->loop);
$this->connector
     ->connect(sprintf('%s:%s', $host, $port))
     ->then(
           function (\React\Socket\ConnectionInterface $conn)
           {
              $conn->on('data', function($data)
              {

              });

              $conn->on('close', function()
              {
                   echo "close\n";
                   $this->loop->addTimer(4.0, function () {
                   $this->connector
                        ->connect('127.0.0.1:8061')
                        ->then( function (\Exception $e)
                        { 
                            echo $e;
                        });
                        });
               });
            });

$this->loop->run();

Exception is empty.

Ivan
  • 91
  • 7

1 Answers1

2

Hey ReactPHP team member here. Promise's then method accepts two callables. The first for when the operation is successful and the second for when an error occurs. Looks like you're mixing both in your example. My suggestion would be to use something like this where you capture errors and successes, but also can reconnect infinitely:

$this->loop = \React\EventLoop\Factory::create();

$this->connector = new \React\Socket\Connector($this->loop);

function connect()
{
  $host = config('app.reactphp_receiver_host');
  $port = config('app.reactphp_receiver_port');
  $this->connector
    ->connect(sprintf('%s:%s', $host, $port))
    ->then(
      function (\React\Socket\ConnectionInterface $conn) { 
        $conn->on('data', function($data) {
        });
        $conn->on('close', function() {
          echo "close\n";
          $this->loop->addTimer(4.0, function () {
            $this->connect();
          });
      }, function ($error) {
        echo (string)$error; // Replace with your way of handling errrors
      }
    );
}

$this->loop->run();
WyriHaximus
  • 1,000
  • 6
  • 8
  • Thank you! It works. And it https://ideone.com/kL1Ylw works too. I want to do infinitely reconnects but the client stops after 1 reconnect. I replaced 'addTimer' to 'addPeriodicTimer'. It doesn`t work. How can I do it? – Ivan Apr 07 '18 at 15:19
  • `addPeriodicTimer` will continue to call `$this->connect();` every 4 seconds. Also you might want to add `$this->connect();` to the error handling methods (also add a `$conn->on('error'` handler) so you reconnect on errors as well. – WyriHaximus Apr 07 '18 at 15:29
  • Yes! It works. Thank you, WyriHaximus. I added: function (\React\Socket\ConnectionInterface $conn) { }, function ($error) { $this->reconnect(); } – Ivan Apr 07 '18 at 15:37