本文整理汇总了PHP中React\EventLoop\LoopInterface::addTimer方法的典型用法代码示例。如果您正苦于以下问题:PHP LoopInterface::addTimer方法的具体用法?PHP LoopInterface::addTimer怎么用?PHP LoopInterface::addTimer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类React\EventLoop\LoopInterface
的用法示例。
在下文中一共展示了LoopInterface::addTimer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: promiseAction
public function promiseAction(Request $request)
{
$secs = intval($request->attributes->get("secs"));
$deferred = new Deferred();
$this->loop->addTimer($secs, function () use($secs, $deferred) {
$deferred->resolve(Response::create("{$secs} seconds later...\n"));
});
return $deferred->promise();
}
示例2: run
/**
* {@inheritdoc}
*/
public function run($tasks, $servers, $environments, $input, $output)
{
$this->tasks = $tasks;
$this->servers = $servers;
$this->input = $input;
$this->output = new OutputWatcher($output);
$this->informer = new Informer($this->output);
$this->port = self::START_PORT;
connect:
$this->pure = new Server($this->port);
$this->loop = $this->pure->getLoop();
// Start workers for each server.
$this->loop->addTimer(0, [$this, 'startWorkers']);
// Wait for output
$this->outputStorage = $this->pure['output'] = new QueueStorage();
$this->loop->addPeriodicTimer(0, [$this, 'catchOutput']);
// Lookup for exception
$this->exceptionStorage = $this->pure['exception'] = new QueueStorage();
$this->loop->addPeriodicTimer(0, [$this, 'catchExceptions']);
// Send workers tasks to do.
$this->loop->addPeriodicTimer(0, [$this, 'sendTasks']);
// Wait all workers finish they tasks.
$this->loop->addPeriodicTimer(0, [$this, 'idle']);
// Start loop
try {
$this->pure->run();
} catch (ConnectionException $exception) {
// If port is already used, try with another one.
$output->writeln("<error>" . $exception->getMessage() . "</error>");
if (++$this->port <= self::STOP_PORT) {
goto connect;
}
}
}
示例3: __construct
public function __construct(LoopInterface $loop, EventEmitter $emit, \SplObjectStorage $clients, RoundProcessor $roundProcessor, EngineHelper $helper, $time = 60, $debug = false)
{
$this->debug = $debug;
$this->helper = $helper;
$this->emit = $emit;
$this->loop = $loop;
$this->roundTimer = new RoundTimer($loop, $emit);
$this->roundNumber = 0;
$this->clients = $clients;
$this->disconnected = new \SplObjectStorage();
$this->time = $time;
$that = $this;
$this->say = new Say($this->clients);
$this->roundProcessor = $roundProcessor;
$players = new Say($this->clients);
// Setup listeners on the roundTimer object.
$emit->on('GAME.COUNTDOWN', function () use($that) {
if ($this->playerCount !== $this->clients->count()) {
$this->playerCount = $this->clients->count();
$this->say->to($this->clients)->that(Say::TICK, ["players" => $this->playerCount]);
}
});
// Setup listeners on the roundTimer object.
$emit->on('GAME.COUNTDOWN_END', function () use($that, $loop, $emit) {
$this->say->to($this->clients)->that(Say::GAME_STARTING, ["players" => $this->clients->count()]);
$this->setGameStarted(true);
$loop->addTimer(3, function () use($loop, $emit) {
$this->startRound($loop, $emit);
});
});
$this->roundTimer->startCountDown($this->time);
}
示例4: add
/**
* Add event listener to event loop.
*
* @param $fd
* @param $flag
* @param $func
* @param array $args
* @return bool
*/
public function add($fd, $flag, $func, $args = array())
{
switch ($flag) {
case EventInterface::EV_READ:
return $this->_loop->addReadStream($fd, $func);
case EventInterface::EV_WRITE:
return $this->_loop->addWriteStream($fd, $func);
case EventInterface::EV_SIGNAL:
return $this->_loop->addSignal($fd, $func);
case EventInterface::EV_TIMER:
return $this->_loop->addPeriodicTimer($fd, $func);
case EventInterface::EV_TIMER_ONCE:
return $this->_loop->addTimer($fd, $func);
}
return false;
}
示例5: enqueueMessage
/**
* @param Player $player
* @param ServerMessageInterface $message
* @param int $timeout
*
* @return Promise
*/
public function enqueueMessage(Player $player, ServerMessageInterface $message, $timeout = 0)
{
$deferred = new Deferred();
$sendMessage = function () use($player, $message, $deferred) {
$connection = $this->getPlayerConnection($player);
if (!$connection) {
$deferred->reject(new \RuntimeException('Connection for player ' . $player->getName() . ' does not exist'));
return;
}
try {
$serializedMessage = $this->serializer->serialize($message);
// Newline is the message separator
$connection->getSocket()->write($serializedMessage . "\n");
$connection->setLastMessageSentAt(new \DateTime());
$deferred->resolve();
} catch (\Exception $e) {
try {
$connection->getSocket()->close();
} catch (\Exception $e) {
}
$deferred->reject($e);
}
};
if ($timeout > 0) {
/** @noinspection PhpParamsInspection */
$this->loop->addTimer($timeout, $sendMessage);
} else {
$sendMessage();
}
return $deferred->promise();
}
示例6: queuePoll
/**
* Sets up a callback to poll a specified feed.
*
* @param string $url Feed URL
*/
protected function queuePoll($url)
{
$self = $this;
$this->loop->addTimer($this->interval, function () use($url, $self) {
$self->pollFeed($url);
});
}
示例7: handleConnectedSocks
public function handleConnectedSocks(Stream $stream, $host, $port, $timeout, $protocolVersion, $auth = null)
{
$deferred = new Deferred();
$resolver = $deferred->resolver();
$timerTimeout = $this->loop->addTimer($timeout, function () use($resolver) {
$resolver->reject(new Exception('Timeout while establishing socks session'));
});
if ($protocolVersion === '5' || $auth !== null) {
$promise = $this->handleSocks5($stream, $host, $port, $auth);
} else {
$promise = $this->handleSocks4($stream, $host, $port);
}
$promise->then(function () use($resolver, $stream) {
$resolver->resolve($stream);
}, function ($error) use($resolver) {
$resolver->reject(new Exception('Unable to communicate...', 0, $error));
});
$loop = $this->loop;
$deferred->then(function (Stream $stream) use($timerTimeout, $loop) {
$loop->cancelTimer($timerTimeout);
$stream->removeAllListeners('end');
return $stream;
}, function ($error) use($stream, $timerTimeout, $loop) {
$loop->cancelTimer($timerTimeout);
$stream->close();
return $error;
});
$stream->on('end', function (Stream $stream) use($resolver) {
$resolver->reject(new Exception('Premature end while establishing socks session'));
});
return $deferred->promise();
}
示例8: queueProcess
protected function queueProcess()
{
if (!$this->benchmarks->valid()) {
return;
}
$b = $this->benchmarks->current();
$this->benchmarks->next();
$key = serialize([$b->benchmark, $b->subbenchmark, $b->count]);
if (!isset($this->stdout[$key])) {
$this->stdout[$key] = '';
}
if (!isset($this->runtimes[$key])) {
$this->runtimes[$key] = [];
}
$current_process_id = $this->process_id;
$this->process_id++;
$this->processes[$current_process_id] = $process = new Process($b->command);
$start = null;
$process->on('exit', function ($code, $signal) use($b, $key, &$current_process_id, &$start) {
$this->runtimes[$key][] = microtime(true) - $start;
$this->output->writeln('<comment>End ' . $b->benchmark . '-' . $b->subbenchmark . ' ' . $b->count . ' #' . $b->execution . '</comment>');
unset($this->processes[$current_process_id]);
$this->queueProcess();
});
$this->loop->addTimer(0.001, function ($timer) use($process, $key, $b, &$start) {
$this->output->writeln('<comment>Start ' . $b->benchmark . '-' . $b->subbenchmark . ' ' . $b->count . ' #' . $b->execution . '</comment>');
$start = microtime(true);
$process->start($timer->getLoop());
$process->stdout->on('data', function ($data) use(&$stdout, $key) {
$this->stdout[$key] .= $data;
});
});
}
示例9: setRequestTimeout
/**
* @param HttpRequest $request
*/
public function setRequestTimeout(HttpRequest $request)
{
if ($this->options['timeout'] > 0) {
$this->requestTimer = $this->loop->addTimer($this->options['timeout'], function () use($request) {
$request->closeError(new \Exception('Transaction time out'));
});
}
}
示例10: run
/**
* Runs the application
*
* @return void
*/
public function run()
{
if (!self::$defaultApplication) {
self::$defaultApplication = $this;
}
$application = $this;
if (OsDetector::isMacOS()) {
$processName = './phpgui-i386-darwin';
$processPath = __DIR__ . '/../lazarus/phpgui-i386-darwin.app/Contents/MacOS/';
} elseif (OsDetector::isFreeBSD()) {
$processName = './phpgui-x86_64-freebsd';
$processPath = __DIR__ . '/../lazarus/';
} elseif (OsDetector::isUnix()) {
$processName = './phpgui-x86_64-linux';
$processPath = __DIR__ . '/../lazarus/';
} elseif (OsDetector::isWindows()) {
$processName = '.\\phpgui-x86_64-win64';
$processPath = __DIR__ . '\\..\\lazarus\\';
} else {
throw new RuntimeException('Operational System not identified by PHP-GUI.');
}
$this->process = $process = new Process($processName, $processPath);
$this->process->on('exit', function () use($application) {
$application->loop->stop();
});
$this->receiver = $receiver = new Receiver($this);
$this->sender = $sender = new Sender($this, $receiver);
$this->loop->addTimer(0.001, function ($timer) use($process, $application, $receiver) {
$process->start($timer->getLoop());
// We need to pause all default streams
// The react/loop uses fread to read data from streams
// On Windows, fread always is blocking
// Stdin is paused, we use our own way to write on it
$process->stdin->pause();
// Stdout is paused, we use our own way to read it
$process->stdout->pause();
// Stderr is paused for avoiding fread
$process->stderr->pause();
$process->stdout->on('data', function ($data) use($receiver) {
$receiver->onData($data);
});
$process->stderr->on('data', function ($data) {
if (!empty($data)) {
Output::err($data);
}
});
$application->running = true;
// Bootstrap the application
$application->fire('start');
});
$this->loop->addPeriodicTimer(0.001, function () use($application) {
$application->sender->tick();
if (@is_resource($application->process->stdout->stream)) {
$application->receiver->tick();
}
});
$this->loop->run();
}
示例11: __construct
public function __construct(LoopInterface $loop)
{
$bspcProcess = proc_open('pactl subscribe', [['pipe', 'r'], ['pipe', 'w']], $pipes);
$bspcStdout = new Stream($pipes[1], $loop);
$bspcStdout->on('data', [$this, 'onUpdate']);
$loop->addTimer(0, function () {
$this->queryData();
});
}
示例12: testFutureTickEventGeneratedByTimer
public function testFutureTickEventGeneratedByTimer()
{
$this->loop->addTimer(0.001, function () {
$this->loop->futureTick(function () {
echo 'future-tick' . PHP_EOL;
});
});
$this->expectOutputString('future-tick' . PHP_EOL);
$this->loop->run();
}
示例13: registerCancelTimer
protected function registerCancelTimer()
{
$this->cancel_timer = $this->loop->addTimer(0.5, function () {
if (false == $this->isAllNeedsProvided()) {
$this->deferred->reject();
}
echo "resolving by timer\n";
$this->deferred->resolve($this->response);
});
}
示例14: __construct
/**
* NewEventLoopScheduler constructor.
* @param callable|LoopInterface $timerCallableOrLoop
*/
public function __construct($timerCallableOrLoop)
{
// passing a loop directly into the scheduler will be deprecated in the next major release
$this->timerCallable = $timerCallableOrLoop instanceof LoopInterface ? function ($ms, $callable) use($timerCallableOrLoop) {
$timerCallableOrLoop->addTimer($ms / 1000, $callable);
} : $timerCallableOrLoop;
parent::__construct($this->now(), function ($a, $b) {
return $a - $b;
});
}
示例15: processQueue
protected function processQueue()
{
$this->loop->addTimer($this->interval, function () {
if ($this->callQueue->isEmpty()) {
return;
}
$message = $this->callQueue->dequeue();
$data = ['function' => $message->getFunction(), 'args' => $message->getArgs(), 'errorResultCode' => $message->getErrorResultCode()];
$message->getDeferred()->resolve($data);
});
}