如何检测到React PHP客户端无法与服务器连接?我希望在底部显示虚构的else()
方法。我是否只需要等待一段合理的时间,并且如果未发生on data
事件,则表示未连接该事件。
<?php
require 'vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$dnsResolverFactory = new React\Dns\Resolver\Factory();
$dns = $dnsResolverFactory->createCached('8.8.8.8', $loop);
$connector = new React\SocketClient\Connector($loop, $dns);
$connector->create('127.0.0.1', 1337)->then(function (React\Stream\Stream $stream) {
$buffer = '';
$stream->on('data', function ($data, $stream) use (&$buffer) {
});
})
/*
->else(function () {
//no connection?
});
*/
$loop->run();
最佳答案
看一下ReactPHP的 PromiseInterface
。为then
使用第二个回调函数,或者使用otherwise
,这正是您的人工else
。
$connector->create('127.0.0.1', 1337)->then(function (React\Stream\Stream $stream) {
$buffer = '';
$stream->on('data', function ($data, $stream) use (&$buffer) {
// ...
});
}, function (Throwable $error) {
// handle error here
});
关于php - 检测React PHP客户端是否无法连接到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43594484/