NotionCommotion Posted May 15, 2017 Share Posted May 15, 2017 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()); } Quote Link to comment Share on other sites More sharing options...
requinix Posted May 16, 2017 Share Posted May 16, 2017 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. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted May 16, 2017 Author Share Posted May 16, 2017 The machine is actively refusing the connection. Timeouts are only for when the machine silently ignores the connection. Ah.. I take it that typical Linux boxes will actively refuse a connection if it doesn't have a process bound to that port. Quote Link to comment Share on other sites More sharing options...
requinix Posted May 16, 2017 Share Posted May 16, 2017 "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. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted May 16, 2017 Author Share Posted May 16, 2017 "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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.