本文整理汇总了PHP中React\EventLoop\LoopInterface::addWriteStream方法的典型用法代码示例。如果您正苦于以下问题:PHP LoopInterface::addWriteStream方法的具体用法?PHP LoopInterface::addWriteStream怎么用?PHP LoopInterface::addWriteStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类React\EventLoop\LoopInterface
的用法示例。
在下文中一共展示了LoopInterface::addWriteStream方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addWriteStream
/**
* Register a listener to be notified when a stream is ready to write.
*
* @param stream $stream The PHP stream resource to check.
* @param callable $listener Invoked when the stream is ready.
*/
public function addWriteStream($stream, callable $listener)
{
$this->emit('addWriteStream', [$stream, $listener]);
$this->loop->addWriteStream($stream, function ($stream) use($listener) {
$this->emit('writeStreamTick', [$stream, $listener]);
$listener($stream, $this);
});
}
示例2: send
/**
* @param string $message
*/
public function send($message)
{
if ($this->closed) {
return;
}
$this->messages[] = $message;
if (!$this->listening) {
$this->listening = true;
$this->loop->addWriteStream($this->fileDescriptor, $this->writeListener);
}
}
示例3: 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;
}
示例4: testRunWaitsForFutureTickEvents
public function testRunWaitsForFutureTickEvents()
{
$stream = $this->createStream();
$this->loop->addWriteStream($stream, function () use($stream) {
$this->loop->removeStream($stream);
$this->loop->futureTick(function () {
echo 'future-tick' . PHP_EOL;
});
});
$this->expectOutputString('future-tick' . PHP_EOL);
$this->loop->run();
}
示例5: onWritable
/**
* Watch a stream resource to become writable and trigger the callback when actionable
*
* @param resource $stream A stream resource to watch for writability
* @param callable $callback Any valid PHP callable
* @param bool $enableNow Should the watcher be enabled now or held for later use?
* @return int
*/
public function onWritable($stream, callable $callback, $enableNow = true)
{
$watcherId = null;
$this->reactor->addWriteStream($stream, function () use($callback, &$watcherId, $stream) {
if (isset($this->disabledWatchers[$watcherId])) {
return null;
}
return call_user_func($callback, $watcherId, $stream, $this);
});
$watcherId = $this->registerWatcher(self::WATCHER_TYPE_WRITE, $stream);
if (!$enableNow) {
$this->disable($watcherId);
}
return $watcherId;
}
示例6: flushWriteBuffer
/**
* Asynchronously sends buffered data over the wire.
*
* - Calls {@link eventLoops}'s addWriteStream() with client's stream.
* - Consecutive calls will return the same instance of promise.
*
* @return Promise\PromiseInterface
*/
protected function flushWriteBuffer()
{
if ($this->flushWriteBufferPromise) {
return $this->flushWriteBufferPromise;
} else {
$deferred = new Promise\Deferred();
$this->eventLoop->addWriteStream($this->getStream(), function ($stream) use($deferred) {
try {
$this->write();
if ($this->writeBuffer->isEmpty()) {
$this->eventLoop->removeWriteStream($stream);
$this->flushWriteBufferPromise = null;
$deferred->resolve(true);
}
} catch (\Exception $e) {
$this->eventLoop->removeWriteStream($stream);
$this->flushWriteBufferPromise = null;
$deferred->reject($e);
}
});
return $this->flushWriteBufferPromise = $deferred->promise();
}
}