public function testSearchTimeout() { $loop = Factory::create(); $client = new Client($loop); $promise = $client->search('ssdp:all', 0.01); $loop->run(); $promise->then($this->expectCallableOnce(), $this->expectCallableNever(), $this->expectCallableNever()); }
/** * Retrieves devices from cache. If not devices are found in cache * then finds/discovers Wemo devices in the network and returns them. * Caches found devices in a file. * * @param bool $refresh Set this to true to force device discovery. * * @return array|mixed|null */ public static function find($refresh = false) { // If not refreshing then look in cache first. if ($refresh === false) { $devices = static::getDevicesFromStorage(); if (!empty($devices)) { return $devices; } else { // No devices found in cache. Force refresh. $refresh = true; } } // Discover devices in the network if ($refresh) { $loop = Factory::create(); $client = new Client($loop); $client->search('urn:Belkin:service:basicevent:1', 2)->then(function () { if (WS::config()->get('debug') === true) { echo 'Search completed' . PHP_EOL; } }, function ($e) { throw new \Exception('Device discovery failed: ' . $e); }, function ($progress) { if (WS::config()->get('debug') === true) { echo "found one!" . PHP_EOL; } static::$output[] = $progress; }); $loop->run(); } // Get additional device info. $devices = static::getDeviceInfo(static::$output); // Cache found devices. static::setDevicesInStorage($devices); return $devices; }
<?php use Clue\React\Ssdp\Client; require __DIR__ . '/../vendor/autoload.php'; $loop = React\EventLoop\Factory::create(); $client = new Client($loop); $client->search()->then(function () { echo 'Search completed' . PHP_EOL; }, function ($e) { echo 'There was an error searching for devices: ' . $e . PHP_EOL; }, function ($progress) { echo 'Found a device: ' . PHP_EOL; var_dump($progress); echo PHP_EOL; }); $loop->run();