Jump to content

reactphp callback questions


Recommended Posts

It is my understanding that I can either use the otherwise method or the second callback, and it is effectively the same thing, right?

 

If a server is not listening to 127.0.0.1:1337, the otherwise method is immediately triggered.  Why doesn't it wait 2 seconds before doing so?

 
Also, if I throw an exception in the otherwise callback, why isn't it caught in the catch block?
 
Thanks
 
<?php


try {
    $loop = \React\EventLoop\Factory::create();
    $connector = new \React\Socket\TimeoutConnector(new \React\Socket\Connector($loop), 2, $loop);
    $connector->connect('127.0.0.1:1337')->then(function (\React\Socket\ConnectionInterface $connection)  use ($loop) {
        $connection->on('data', function($data) use ($loop, $connection){});
        $connection->on('error', function($error, $connection) use ($loop){
            $connection->close();                                                      
            $loop->stop();
            throw new \Exception('onError');
        });
        $connection->write('Hello');
        } /*, function (\RuntimeException $error) use ($loop) {
            //It is my understanding that this second callback is the same thing as using the otherwise method
            $loop->stop();
            throw new \Exception('onSecondCallback');
    }*/)->otherwise(function(\RuntimeException $error) use($loop){
        $loop->stop();
        throw new \Exception('onOtherwise');
    });
    $loop->run();
    echo('will this be executed on exception');
} catch (\Exception $exception) {
    echo($exception->getMessage());
}
 

 

 

Link to comment
Share on other sites

It is my understanding that I can either use the otherwise method or the second callback, and it is effectively the same thing, right?

Seems so. Check the documentation and even the code itself. Personally I would go with otherwise() as that fits the React paradigm.

 

If a server is not listening to 127.0.0.1:1337, the otherwise method is immediately triggered.  Why doesn't it wait 2 seconds before doing so?

The machine is actively refusing the connection. Timeouts are only for when the machine silently ignores the connection.

 

Also, if I throw an exception in the otherwise callback, why isn't it caught in the catch block?

Seems like it's being caught somewhere else. Check logs. If the catcher is using getMessage() then you can

class skldhgbvnException extends \Exception {
	public function getMessage() {
		debug_print_backtrace();
		return parent::getMessage();
	}
}
to find out where.
Link to comment
Share on other sites

"Typical" is debatable (especially on Windows) but yes: it's common for computers to reject connections they don't know what to do with instead of dropping the connection. Instead, dropping is almost universal for traffic on the external port of a router.

Link to comment
Share on other sites

"Typical" is debatable (especially on Windows) but yes: it's common for computers to reject connections they don't know what to do with instead of dropping the connection. Instead, dropping is almost universal for traffic on the external port of a router.

 

 

Thanks!

 

Note to self.  Consider implementing something which is more silent if a router doesn't exist between the machine and the unknown.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.